Setting a control to readonly using jquery 1.6 .prop()

ajbeaven picture ajbeaven · May 5, 2011 · Viewed 35.1k times · Source

With the release of jQuery 1.6, the recommendation on SO has been to generally start using prop() where you used to use attr().

What happens when I want make an element readonly?

$('.control').prop('readonly', 'readonly');
$('.control').prop('readonly', true);

Neither of these seem to make the control readonly. Is making an element readonly the exception to the rule?

Answer

aroth picture aroth · May 5, 2011

The problem is that the property name is case-sensitive. Try:

$('.control').prop('readOnly', true);

Though really I don't know why this requires jQuery. This works just as well:

document.getElementsByClassName("control")[0].readOnly = true;