• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Option Constructor

 
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Sheriff
    Posts: 67750
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Sheriff
    Posts: 67750
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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:

    reply
      Bookmark Topic Watch Topic
    • New Topic