Using a Variable for the Selection

A variable is a temporary holder of information, such as the user’s selection in a drop-down box. Using variables clarifies the programming logic, making it easier to troubleshoot. The table below lists the characteristics of a variable.

CharacteristicExplanation

Name

Names must start with a letter or underscore. Subsequent characters may include letters, numbers, or the underscore (drp_Territory is a legal name). A variable name cannot be a reserved word (new is a reserved word, but newVariable is not).

Use a JavaScript var statement to declare a variable name before it is used. For example:

var newVariable;

In the above statement, JavaScript reserves the name newVariable, with a value of null or undefined.

Value

Values are assigned to variables with the JavaScript assignment operator (=). Any type of data can be assigned to a variable: an object (a string, a user selection, an object path), a boolean (true or false), or a number. For example:

var newVariable;
newVariable=true;

The value can be assigned in a separate statement like above, or in the same statement that declares the variable name. For example:

var newVariable=true;

newVariable (and its value) is available only within the script or function that declares it, preventing accidental changes from other scripts using the same variable name.

This exercise uses a variable to hold the user’s selection (the first line of the script from the preceding exercise). A variable can be declared in one statement and the value assigned in another:

var Selection;
Selection=Territory.Item(Territory.SelectedIndex);

or the statements can be combined:

var Selection=Territory.Item(Territory.SelectedIndex);

Note:

The following exercise continues from the previous section. This exercise alters the script in the drop-down box (renamed drp_Territory) in the Limits Dashboard section of Sample1mod.bqy.