JavaScripTools Manual

JavaScriptUtil

Manipulating options on a select box

JavaScripTools offers the following functions to manipulate the options inside a select field.

addOption and addOptions functions add one or more options.

Examples (assuming a select box named choices):
//Using the standard Option class
addOption("choices", new Option("Zero", "0"));
//Using an object with text and value properties
addOption("choices", {text:"One", value:"1"});
//Using an object with custom properties
addOption("choices", {description:"One", id:"1"}, "description", "id");
//All 3 options are added
addOptions("choices", [new Option("Two", "2"), new Option("Three", "3"), new Option("Four", "4"));

setOptions replaces all existing options with the specified ones.

Examples (assuming a select box named choices):
//Now 'choices' has only these 2 options, no matter the prior state
setOptions("chosen", [new Option("Ten", "10"), new Option("Twenty", "20")); 

clearOptions removes all existing options.

Examples (assuming a select box named choices):
clearOptions("chosen"); 

sortOptions sorts all options by text.

Examples (assuming a select box named choices):
sortOptions("chosen"); 

Controls called 'shuttle', are two select boxes, one containing a list of all possible choices and the other only the selected choices. Normally, there are also 4 buttons: Transfer all from right to left, the selected from right to left, the selected from left to right and all from left to right.

To allow those operations, there is a special function: transferOptions

Examples (assuming a select box named choices and another named chosen):
//Transfer all options from 'choices' to 'chosen' (no sorting)
transferOptions("choices", "chosen", true); 
//Transfer the selected options from 'choices' to 'chosen', sorting by text
transferOptions("choices", "chosen", false, true);




Previous:
Retrieving and setting values on form fields and html elements
Table of Contents Next:
Manipulating caret and selection on input fields