Java Reference
In-Depth Information
You then simply assign this Option object to an empty array element, for example:
document.theForm.theSelectObject.options[0] = myNewOption;
If you want to remove an option, you simply set that part of the options collection to null. For example,
to remove the element you just inserted, you need the following:
document.theForm.theSelectObject.options[0] = null;
When you remove an Option object from the options collection, the collection is reordered so that the
array index value of each of the options above the removed one has its index value decremented by one.
When you insert a new option at a certain index position, be aware that it will overwrite any Option
object that is already there.
Try It Out Adding and Removing List Options
Use the list-of-days example you saw previously to demonstrate adding and removing list options.
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Chapter 7: Example 7</title>
<script type=”text/javascript”>
function btnRemoveWed_onclick()
{
if (document.form1.theDay.options[2].text == “Wednesday”)
{
document.form1.theDay.options[2] = null;
}
else
{
alert('There is no Wednesday here!');
}
}
function btnAddWed_onclick()
{
if (document.form1.theDay.options[2].text != “Wednesday”)
{
var indexCounter;
var days = document.form1.theDay;
var lastoption = new Option();
days.options[days.options.length] = lastoption;
for (indexCounter = days.options.length - 1;
indexCounter > 2; indexCounter--)
{
days.options[indexCounter].text =
days.options[indexCounter - 1].text;
days.options[indexCounter].value =
days.options[indexCounter - 1].value;
}
var option = new Option(“Wednesday”, 2);
days.options[2] = option;
Search WWH ::




Custom Search