Read-only properties in HTMLOptionElement objects

Discussion in 'Javascript' started by Michael Winter, Jan 21, 2004.

  1. 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
    d (replace ".invalid" with ".uk" to reply)
    Michael Winter, Jan 21, 2004
    #1
    1. Advertising

  2. 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-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.
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jan 21, 2004
    #2
    1. Advertising

  3. On Wed, 21 Jan 2004 18:06:29 +0100, Martin Honnen <>
    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
    d (replace ".invalid" with ".uk" to reply)
    Michael Winter, Jan 21, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. K B
    Replies:
    0
    Views:
    543
  2. Victor Porton
    Replies:
    1
    Views:
    698
    Steven T. Hatton
    Aug 29, 2004
  3. keithb
    Replies:
    2
    Views:
    8,007
    keithb
    Jun 7, 2006
  4. Greg Brunet

    Static class properties (read-only)

    Greg Brunet, Aug 7, 2003, in forum: Python
    Replies:
    3
    Views:
    497
    Alex Martelli
    Aug 7, 2003
  5. Paul Moore
    Replies:
    9
    Views:
    601
    Roy Smith
    Feb 21, 2005
Loading...

Share This Page