11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Table 14-11: Additional Properties of the HTMLOptionElement Object<br />

Property Description<br />

Text String holding the text found enclosed by the opening and closing<br />

tags. This is often confused with value; since the text<br />

enclosed by the tags is sent to the server, its value is not<br />

specified.<br />

value String holding the text of the value attribute, which will be sent to the<br />

server if the option is selected at submission time.<br />

Scripting Select Menus<br />

<strong>The</strong> fact that form fields are scriptable means that you can affect the appearance or content of<br />

one element in response to actions users perform on another. We‘ll see this technique several<br />

times later in this chapter, but perhaps the most common application is with select menus.<br />

Related select menus provide the ability to present a large number of options very quickly to the<br />

user. <strong>The</strong> key to building such menus in <strong>JavaScript</strong> is understanding how to edit or even add<br />

new s to a menu on the fly. <strong>The</strong> traditional way to do this in <strong>JavaScript</strong> is to use the<br />

new operator on the Option() constructor, and then insert the resulting option into the menu.<br />

<strong>The</strong> Option constructor syntax is<br />

var newOption = new Option(optionText, optionvalue);<br />

where optionText is a string holding the text enclosed by the opening and closing <br />

tags and optionValue is a string specifying the element‘s value attribute.<br />

Once created, Option objects can be inserted into the options[] collection for a select menu.<br />

You can delete any unused entries by setting their values to null. <strong>The</strong> following simple example<br />

provides two menus, one with a country and another with a list of cities. <strong>The</strong> city menu will<br />

change dynamically when the user chooses a country.<br />

<br />

<br />

<br />

Related Select Test<br />

<br />

<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!