![]() |
Removing options from select based on text
I have this bit of javascript:
Code:
selectBox = document.getElementById("IndexDropdown");Code:
<select onchange="SetPlayerPosition(this.options[this.selectedIndex].value,this)" style="width:100%" id="IndexDropdown"> |
This is because your loop changes as you remove the nodes (i.e. selectBox.options indicies changed as soon as the first node was removed). You have to put all your deleted nodes into an array and then loop through that like this:
selectBox = document.getElementById("IndexDropdown"); deletedNodes = []; selectBoxLength = selectBox.options.length; badVar = "ORAL"; for (i = 0; i < selectBoxLength; i++) { if ( /ORAL/.test(selectBox.options[i].innerHTML) ) { deletedNodes.push(selectBox.options[i]); } } for (i = 0; i < deletedNodes.length; i++) { selectBox.removeChild(deletedNodes[i]); } |
Another option is to loop through the options starting from the last one and working back to the first option.
Then delete each option that contains badVar apart from the first occurence which will be the last option with badVar in it. Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
Quote:
using while() PHP Code:
PHP Code:
|
You must remove the option using a backwards loop, in which case the length of the collection is irrelevant, as it will not affect the next element to be removed in a series.
Code:
for (var i=selectBox.options.length-1; i>=0; i--) { |
@op
I misread your initial post. I've removed the counter from my initial post Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
| All times are GMT +1. The time now is 11:17 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.