I am using the following statement to make it readonly but it is not working.
$(\'#cf_1268591\').attr(\"readonly\", \"readonly\");
I don\
This is what you are looking for:
$('#cf_1268591').attr("style", "pointer-events: none;");
Works like a charm.
Setting an element with disabled
will not submit the data, however select
elements don't have readonly
.
You can simulate a readonly
on select
using CSS for styling and JS to prevent change with tab:
select[readonly] {
background: #eee;
pointer-events: none;
touch-action: none;
}
Then use it like:
var readonly_select = $('select');
$(readonly_select).attr('readonly', true).attr('data-original-value', $(readonly_select).val()).on('change', function(i) {
$(i.target).val($(this).attr('data-original-value'));
});
Result:
// Updated 08/2018 to prevent changing value with tab
$('a').on('click', function() {
var readonly_select = $('select');
$(readonly_select).attr('readonly', true).attr('data-original-value', $(readonly_select).val()).on('change', function(i) {
$(i.target).val($(this).attr('data-original-value'));
});
});
select[readonly] {
background: #eee;
pointer-events: none;
touch-action: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#">Click here to enable readonly</a>
<select>
<option>Example 1</option>
<option selected>Example 2</option>
<option>Example 3</option>
</select>
You could also disable it and at the moment that you make the submit call enable it. I think is the easiest way :)