Read-only properties in HTMLOptionElement objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Winter

    #1

    Read-only properties in HTMLOptionElement objects

    Below is the IDL definition of the HTMLOptionEleme nt.

    interface HTMLOptionEleme nt : 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.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)
  • Martin Honnen

    #2
    Re: Read-only properties in HTMLOptionEleme nt objects



    Michael Winter wrote:
    [color=blue]
    > Below is the IDL definition of the HTMLOptionEleme nt.
    >
    > interface HTMLOptionEleme nt : 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].[/color]

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

    interface HTMLOptionEleme nt : 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.
    [color=blue]
    > 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?[/color]

    No, I am sure Opera 7 implements some readonly requirements of the W3C
    DOM HTML module, for instance try
    var button = document.create Element('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/

    Comment

    • Michael Winter

      #3
      Re: Read-only properties in HTMLOptionEleme nt objects

      On Wed, 21 Jan 2004 18:06:29 +0100, Martin Honnen <mahotrash@yaho o.de>
      wrote:
      [color=blue]
      > Michael Winter wrote:[/color]

      <snip>
      [color=blue]
      > 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.[/color]

      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.
      [color=blue]
      > 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.[/color]

      I could have just tested it, but I didn't want to just assume that "if it
      works here, it'll work elsewhere".
      [color=blue][color=green]
      >> 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?[/color]
      >
      > No, I am sure Opera 7 implements some readonly requirements of the W3C
      > DOM HTML module, for instance try
      > var button = document.create Element('button ');
      > button.type = 'reset';
      > and you should get an error as unfortunately the DOM even in level 2
      > marks that property as readonly.[/color]

      I do. Luckly, you can use the Element.setAttr ibute() method.
      [color=blue]
      > 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.[/color]

      <snip>

      That was my assumption as well.

      Mike

      --
      Michael Winter
      M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

      Comment

      Working...