This is my code inside document.ready:
var $inputs = mySelectors();
$inputs.each(function() {
$(this).attr("readonly", "true");
});
This code works for IE8, but not for FF3.5
IE output(as seen with IE Developer toolbar)<input type="text" readOnly="readonly"..../>
FF output (seen with Firebug)
<input type="text" readonly="">
What is the right way to set it ?
$elem.attr("readonly","true");
or
$elem.attr("readonly","readonly");
or
$elem.attr("readOnly","readonly"); //note the uppercase O in Only
Looks like there was an old bug but not sure if it got resolved. http://osdir.com/ml/jquery-dev/2009-05/msg00115.html
reference:http://api.jquery.com/attr/
Cross-browser consistency: Some attributes have inconsistent naming from browser to browser. Furthermore, the values of some attributes are reported inconsistently across browsers, and even across versions of a single browser. The .attr() method reduces such inconsistencies.
IS there a way to tackle this cross browser inconsistency ?
The functionality for getting/setting properties (as opposed to setting attributes) were fundamentally changed in jQuery 1.6. As of jQuery 1.6.1 it's still possible to get/set these boolean properties using
.attr('readonly')
, however the preferred usage isThis change was promoted by the ambiguity of attributes (which reflect the inital state of the element) and properties (which represent the current state of the DOM node).
For more information: http://blog.jquery.com/2011/05/12/jquery-1-6-1-released/
If it were my code, I'd probably set it to plain boolean "true":
When you say that it doesn't work; what exactly happens?
Here's a sample script: http://gutfullofbeer.net/readonly.html
That one uses my method above, and it works just fine in Firefox for me. Firebug shows the attribute like that because it feels like it.
This should be the right way:
This way, you create valid XHTML as every attribute has to have a value and element and attribute names have to be lower case. For most boolean attributes that means that the value is the same as the name.
See also here:
And also this remark:
Or you change the document type ;)
This is one of those irritating minute differences between IE and FF syntax. IE uses camel case where FF does not.
IE syntax: readOnly="readonly"
FF syntax: readonly="readonly"