04.11.2015 Views

javascript

Create successful ePaper yourself

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

Chapter 13: Scripting Forms<br />

In this example, the selected options are retrieved from a select box. A for loop is used to construct a<br />

message containing information about all of the selected options, including each option’s index, text, and<br />

value. This can be used for select boxes that allow single or multiple selection.<br />

Adding Options<br />

There are several ways to create options dynamically and add them to select boxes using JavaScript. The<br />

first way is to the use the DOM as follows:<br />

var newOption = document.createElement(“option”);<br />

newOption.appendChild(document.createTextNode(“Option text”));<br />

newOption.setAttribute(“value”, “Option value”);<br />

selectbox.appendChild(newOption);<br />

This code creates a new < option > element, adds some text using a text node, sets its value attribute,<br />

and then adds it to a select box. The new option shows up immediately after being created.<br />

New options can also be created using the Option constructor, which is a holdover from pre - DOM<br />

browsers. The Option constructor accepts two arguments, the text and the value , though the second<br />

argument is optional. Even though this constructor is used to create an instance of Object , DOM -<br />

compliant browsers return an < option > element. This means you can still use appendChild() to add<br />

the option to the select box. Consider the following:<br />

var newOption = new Option(“Option text”, “Option value”);<br />

selectbox.appendChild(newOption); //problems in IE<br />

This approach works as expected in all browsers except IE. Due to a bug, IE doesn ’ t set the text of the<br />

new option correctly when using this approach.<br />

Another way to add a new option is to use the select box ’ s add() method. The DOM specifies that this<br />

method accepts two arguments: the new option to add and the option before which the new option<br />

should be inserted. To add an option at the end of the list, the second argument should be null .<br />

The IE implementation of add() is slightly different in that the second argument is optional and it must<br />

be the index of the option before which to insert the new option. DOM - compliant browsers require the<br />

second argument, so you can ’ t use just one argument for a cross - browser approach. Instead, passing<br />

undefined as the second argument assures that the option is added at the end of the list in all browsers.<br />

Here’s an example:<br />

var newOption = new Option(“Option text”, “Option value”);<br />

selectbox.add(newOption, undefined); //best solution<br />

This code works appropriately in IE as well as DOM - compliant browsers. If you need to insert a new<br />

option into a position other than last, you should use the DOM technique and insertBefore() .<br />

As in HTML, you are not required to assign a value for an option. The Option<br />

constructor works with just one argument (the option text).<br />

454

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

Saved successfully!

Ooh no, something went wrong!