Javascript Reference
Categories: OPTION

javascript OPTION Description

@Aug. 17, 2009, 8:49 a.m.
option Firefox/Netscape/NN 2 IE 3 Chrome/Safari/DOM 1  



The option object reflects the option element, which must be nested inside a select element. References to option objects most often use its parent select object, with the option object treated as one member of an array of options belonging to that select object. With modern browsers, you can also reference an option object directly via its ID. The disabled property is available for IE 4 and later and Netscape 6.


You can modify the set of options in a select object in browsers starting with Netscape 3 and Explorer 4 with backward-compatible code that continues to work in the newest browsers. If the modification entails replacing existing options with a different list of the same length, you can simply assign new values to text, value, and selected properties of each option in the select object's options array. But if the list has a different number of options, you are better served by removing all existing option objects and inserting new ones. A constructor function for a new Option object lets you create objects one at a time, and then assign them to positions within the options array. Syntax for the constructor is as follows:

var newOpt = new Option("text", "value", isDefaultSelectedFlag, 

The following function demonstrates the typical steps involved in rewriting a select object's list of options:

function setSelect(selectElemObj) {
  // remove existing options
  selectElemObj.options.length = 0;
  // create and assign options, one by one
  selectElemObj.options[0] = new Option("Hercule Poirot", "poirot", false, false);
  selectElemObj.options[1] = new Option("Miss Marple", "marple", false, false);

In a production environment, the values for the constructor parameters would most likely be delivered to the page as an array of objects, allowing the stuffing of new options to be carried out inside a for loop. For additional approaches to this task, see the options.add( ) method (for IE only) and the select.add( ) method (for IE 5 or later and Netscape 6 only).

HTML Equivalent
Object Model Reference
Object-Specific Properties
defaultSelected form index label selected text value
Object-Specific Methods


Object-Specific Event Handler Properties


Powered by Linode.