Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove option in a select

Status
Not open for further replies.

specv

Programmer
Aug 8, 2001
42
CA
Is it possible to remove an option in a select for both netscape and ie?

This code works whith ie but not with netscape :
my_listbox.options.remove(my_index)

Thanks

Phil
 
this should work
Code:
function removeSelectOption(objSelect, strValue)
{
	for (i=0;i<objSelect.options.length;i++)
	{
		if (objSelect.options[i].value == strValue)
		{
			objSelect.options[i] = null;
		}
	}
} //removeSelectOption
called like removeSelectOption(document.formName.SelectBoxName, ValueToDelete);
 
Setting the value to null will work but make sure that you lesson the loop on each iteration that you remove a option. Such as
function removeSelectOption(objSelect, strValue)
{
for (i=0;i<objSelect.options.length;i++)
{
if (objSelect.options.value == strValue)
{
objSelect.options = null;
i--;
}
}
} //removeSelectOption

This may not matter in you particular case, but I ran into this problem when trying to remove multiple options at once. Because every time you remove an option, the options array becomes 1 smaller. And if you try to set an option to null that no longer exists it will throw an error in certain browsers. Just something to keep in mind.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top