spacer
Yehuda Shiran January 18, 2000
The Option Constructor
Tips: January 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

The Option object is contained by the Select object. The Select object may have many options. They are contained in its options[] array. Here is an example for a reference to an option:

document.forms[3].elements[2].options[5]

The option objects can be create dynamically at runtime, via the Option constructor. The syntax for creating an option is as follows:

var optionObj = new Option([optionText, optionValue, defaultSelected, selected]);

where:

  • optionText is a string representing the option's text property
  • optionValue is a string representing the option's value property
  • defaultSelected is a string representing the option's defaultSelected property
  • selected is a string representing the option's selected property

You can add an option to an existing select object in the following fashion:

selectman.options[index] = optionObj;

After you create an option and add it to a Select object, you must refresh the document. One way to refresh the document is by using the following JavaScript statement:

history.go(0)

You can also delete an option from a Select object, by assigning it a null value. The general syntax will then be:

selectName.options[index] = null;


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business