469,288 Members | 2,353 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,288 developers. It's quick & easy.

Read-only properties in HTMLOptionElement objects

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.

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #1
2 3188


Michael Winter wrote:
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-HTM...ml#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.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
On Wed, 21 Jan 2004 18:06:29 +0100, Martin Honnen <ma*******@yahoo.de>
wrote:
Michael Winter wrote:
<snip>
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".
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 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

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Gunnar | last post: by
6 posts views Thread by Steve | last post: by
12 posts views Thread by Steven T. Hatton | last post: by
2 posts views Thread by Sandman | last post: by
4 posts views Thread by Ollie Cook | last post: by
1 post views Thread by Jose Reckoner | last post: by
6 posts views Thread by arnuld | last post: by
4 posts views Thread by zl2k | last post: by
5 posts views Thread by Thomas Christensen | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.