Java Reference
In-Depth Information
Using the add() method, you can rewrite the btnAddWed_onclick() function in your
ch7.examp7.htm example to look like this:
function btnAddWed_onclick()
{
var days = document.form1.theDay;
if (days.options[2].text != “Wednesday”)
{
var option = new Option(“Wednesday”, 2);
var thursdayOption = theDay.options[2];
try
{
days.add(option, thursdayOption);
}
catch (error)
{
days.add(option, 2);
}
}
else
{
alert(“Do you want to have TWO Wednesdays?????”);
}
}
In IE7 (or IE8 in non-standards mode), the browser will throw an error if you pass an Option object as
the second parameter. So use a try...catch statement to catch the error and pass a number to the sec-
ond argument, as this code shows.
The Select object's remove() method accepts just one parameter, namely the index of the option you
want removed. When an option is removed, the options at higher index positions are moved down in
the collection to fi ll the gap.
Using the remove() method, you can rewrite the btnRemoveWed_onclick() function in your
ch7_examp7.htm example to look like this:
function btnRemoveWed_onclick()
{
var days = document.form1.theDay;
if (days.options[2].text == “Wednesday”)
{
days.remove(2);
}
else
{
alert(“There is no Wednesday here!”);
}
}
Modify the previous example and save it as ch7_examp8.htm before loading it into your browser.
You'll see that it works just as the previous version did.
Search WWH ::




Custom Search