This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Option Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Option Constructor" Watch "Option Constructor" New topic
Author

Option Constructor

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

    Joined: Jan 28, 2004
    Posts: 569
    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

    Joined: Jan 10, 2002
    Posts: 60810
        
      65

    The HTML DOM is not part of the language specification.

    Here's the horse's mouth.
    [ August 07, 2007: Message edited by: Bear Bibeault ]

    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Alec Lee
    Ranch Hand

    Joined: Jan 28, 2004
    Posts: 569
    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

    Joined: Jan 10, 2002
    Posts: 60810
        
      65

    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:

     
    Don't get me started about those stupid light bulbs.
     
    subject: Option Constructor
     
    Similar Threads
    serialized
    Question on Construtor
    doubt in Array construction
    Mock Question - Bean Standard Action
    call super class constructor