[Javascript] readonly in IE7

liorean liorean at gmail.com
Sun Sep 2 07:28:02 CDT 2007


On 01/09/07, John Pillion <john at conv-dg.com> wrote:
> I saw elsewhere that the following sometimes is more reliable for IE to
> do the following:
>
>     txtBox.setAttribute('readonly','');
>     txtBox.readonly = 'readonly';
>
> this does not work either though, nor does
>
>     txtBox.readonly = 'readonly';

First of all, have a look at these two resources:
<uri:http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML-20030109/html.html#ID-6043025>
<uri:http://msdn2.microsoft.com/en-us/library/ms534357.aspx>

As you can see, the property name in the DOM bindings is elm.readOnly,
not elm.readonly. (And the DOM APIs as well as JS are case sensitive.)

Second, the property is not a string property, it's a boolean property.

That is, the HTML <elm ... readonly ...> or <elm ...
readonly="readonly" ...> corresponds to the JS elm.readonly=true. The
HTML element not having the readonly attribute corresponds to JS
elm.readonly=false.





In general, it's a bad idea using elm.setAttribute in iew, since the
Microsoft implementation is seriously broken/buggy/misimplemented.
Using the DOM0 bindings is safer.

Also, in general it's a bad idea to use setAttribute if you're
interested in side effects of the attribute rather than the attribute
being on the element and containing the right text. The DOM does
nowhere promise that changing the DOM attribute values will actually
have side effects. An example of this is that setting an event handler
attribute through the DOM doesn't actually parse that into a function
and attach that function as an event handler on the element in some
browsers.
-- 
David "liorean" Andersson



More information about the Javascript mailing list