Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
53
rated 0 times [  55] [ 2]  / answers: 1 / hits: 193497  / 12 Years ago, wed, october 17, 2012, 12:00:00

I have the following in the page



<select name=val size=1 >
<option value=A>Apple</option>
<option value=C>Cars</option>
<option value=H>Honda</option>
<option value=F>Fiat</option>
<option value=I>Indigo</option>
</select>


I would like to remove certain values from my select if certain conditions are true.



E.g



if(frm.product.value==F){
// remove Apple and Cars from the select list
}


How can I do this using javascript


More From » html

 Answers
1

Give an id for the select object like this:



<select id=mySelect name=val size=1 >
<option value=A>Apple</option>
<option value=C>Cars</option>
<option value=H>Honda</option>
<option value=F>Fiat</option>
<option value=I>Indigo</option>
</select>


You can do it in pure JavaScript:



var selectobject = document.getElementById(mySelect);
for (var i=0; i<selectobject.length; i++) {
if (selectobject.options[i].value == 'A')
selectobject.remove(i);
}


But - as the other answers suggest - it's a lot easier to use jQuery or some other JS library.


[#82505] Tuesday, October 16, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
manuel

Total Points: 747
Total Questions: 96
Total Answers: 95

Location: Argentina
Member since Thu, Mar 18, 2021
3 Years ago
;