Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Option Constructor

 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we need to dynamically create options for a Select element we would use the "new Option(..)" constructor to create a new option object. However, where is this Option constructor defined? Is it in the "window" global object?

Secondly, it seems that both ECMAScript and W3C DOM standard doesn't define how this constructor should look like (e.g. what arguments it should take). Which standard does this Option() constructor come from?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sure it is in the ECMAScript pdf, too lazy to find the exact location in the document.

The optional parameters for Option are the following

new Option([text[, value[, defaultSelected[, selected]]]])

  • text (required) - value shown on page
  • value - the value
  • defaultSelected - [true/false] - this is equivilavnt of adding the selected attribute to the element on the html tag. So if you reset a form, the value would remain selected.
  • selected - [true/false] - selects the value when option is set to true


  • Eric
     
    Alec Lee
    Ranch Hand
    Posts: 569
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Are you referring to this document?

    http://www.ecma-international.org/publications/standards/Ecma-262.htm

    I did search for the keyword "option" and there isn't anything looks like the one needed. I have the impression that this is DOM Level 0 stuff. But since this is the constructor function, I am not able to locate the relevant information from either msdn or mozilla's documentation. Could anyone point me to the right direction.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 65225
    95
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The HTML DOM is not part of the language specification.

    Here's the horse's mouth.
    [ August 07, 2007: Message edited by: Bear Bibeault ]
     
    Alec Lee
    Ranch Hand
    Posts: 569
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Bear,

    I don't think the Optionconstructor is part of w3c's DOM as the HTMLSelectElement.options of DOM Level2 is defined as HTMLOptionsCollection. That is, the W3C's way of adding new options is to add some new Nodes to the DOM tree rather than using simple new Option(..) syntax.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 65225
    95
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What on earth are you talking about?

    Of course you need to create the option element with new Option() before adding it to the select element. This is all defined by the DOM spec.

    And of course IE, gums up the works by being non-standard (with regards to the seccond parameter to the add method).

    Standards compliant code would be something like:

     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic