Read-only properties in HTMLOptionElement objects

M

Michael Winter

Below is the IDL definition of the HTMLOptionElement.

interface HTMLOptionElement : HTMLElement {
readonly attribute HTMLFormElement form;
attribute boolean defaultSelected;
readonly attribute DOMString text;
attribute long index;
attribute boolean disabled;
attribute DOMString label;
readonly attribute boolean selected;
attribute DOMString value;
};

You will notice that the 'text' and 'selected' properties are marked as
read-only. This adds an interesting dilemma: how do you go about
programatically modifying these properties, especially considering the
need to assign to 'text' when creating a new object? Would you add a text
node, as you find in HTML, to represent the text displayed? If that is the
correct approach, it still leaves 'selected' inaccessible[1].

Something that I did notice is that the ECMAScript bindings don't mark any
properties as read-only, and assigning to 'text' is possible, certainly in
Opera. Is that because there is no concept of read-only, so the browser
can choose to ignore assignments, or some other reason?


Mike

[1] I realise that you can use the 'selectedIndex' property to select
single options, but that isn't of much use when the SELECT element can
have multiple selected options.
 
M

Martin Honnen

Michael said:
Below is the IDL definition of the HTMLOptionElement.

interface HTMLOptionElement : HTMLElement {
readonly attribute HTMLFormElement form;
attribute boolean defaultSelected;
readonly attribute DOMString text;
attribute long index;
attribute boolean disabled;
attribute DOMString label;
readonly attribute boolean selected;
attribute DOMString value;
};

You will notice that the 'text' and 'selected' properties are marked as
read-only. This adds an interesting dilemma: how do you go about
programatically modifying these properties, especially considering the
need to assign to 'text' when creating a new object? Would you add a
text node, as you find in HTML, to represent the text displayed? If that
is the correct approach, it still leaves 'selected' inaccessible[1].

The W3C DOM Level 2 HTML module at
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-70901257
contains

interface HTMLOptionElement : HTMLElement {
readonly attribute HTMLFormElement form;
// Modified in DOM Level 2:
attribute boolean defaultSelected;
readonly attribute DOMString text;
// Modified in DOM Level 2:
readonly attribute long index;
attribute boolean disabled;
attribute DOMString label;
attribute boolean selected;
attribute DOMString value;
};

You haven't told us which version you are referring to but the above is
up to date and doesn't mark the selected attribute (or property in
JavaScript terms) as readonly.
As for the text, yes, you should be able to append a text node as a
child node to set that, IE5+, Netscape 6+, Opera 7 allow that.
Something that I did notice is that the ECMAScript bindings don't
mark any properties as read-only, and assigning to 'text' is possible,
certainly in Opera. Is that because there is no concept of read-only, so
the browser can choose to ignore assignments, or some other reason?

No, I am sure Opera 7 implements some readonly requirements of the W3C
DOM HTML module, for instance try
var button = document.createElement('button');
button.type = 'reset';
and you should get an error as unfortunately the DOM even in level 2
marks that property as readonly.

I think the reason that it is possible to set text for <option> elements
is historical as browser have allowed that with
var option = new Option();
option.text = 'option text';
even before there was any W3C DOM. But of course that is a guess, only
someone at Opera can answer that for Opera, for Mozilla you might want
to search bugzilla whether text was readonly at sometime and a bug
requesting to change that for NN4 compatibility was accepted and fixed.
 
M

Michael Winter

Michael Winter wrote:

You haven't told us which version you are referring to but the above is
up to date and doesn't mark the selected attribute (or property in
JavaScript terms) as readonly.

Sorry, I was referring to DOM 1 (as you probably guessed). I was going to
check if they had made any changes to Level 2.
As for the text, yes, you should be able to append a text node as a
child node to set that, IE5+, Netscape 6+, Opera 7 allow that.

I could have just tested it, but I didn't want to just assume that "if it
works here, it'll work elsewhere".
No, I am sure Opera 7 implements some readonly requirements of the W3C
DOM HTML module, for instance try
var button = document.createElement('button');
button.type = 'reset';
and you should get an error as unfortunately the DOM even in level 2
marks that property as readonly.

I do. Luckly, you can use the Element.setAttribute() method.
I think the reason that it is possible to set text for <option> elements
is historical as browser have allowed that with
var option = new Option();
option.text = 'option text';
even before there was any W3C DOM.

<snip>

That was my assumption as well.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top