I have a number of dynamic <select> elements that are generated based on various user inputs. I am trying to remove specific <option> that are not applicable for each choice, but jQuery seems to remove more than <option> , then it should be removed based on my code. The full use case will be difficult to reproduce, so I will post what I see and hopefully it will be good enough to understand this.
To prove to myself that I'm not crazy, I decided to add a class instead of removing the <option> in the loop. Here is what I used:
$('.my-select').each(function(sourceIdx,sourceEl){ var id = $(sourceEl).attr('id'); var s = $('#'+id).parents('table').find('.some-input').val(); $(sourceEl).find('option[value="'+s+'"]').addClass('option-remove'); });
Here's the generated source:
<select class="my-select" id="1295453461993"> <option title="1" value="1">1</option> <option title="2" value="2" class="option-remove">2</option> <option title="3" value="3">3</option> </select> <select class="my-select" id="1295453475890"> <option title="1" value="1">1</option> <option title="2" value="2">2</option> <option title="3" value="3" class="option-remove">3</option> </select>
That is exactly what I want. However, when I add this immediately after the loop:
$('.option-remove').remove();
I get this:
<select class="my-select" id="1295454051124"> <option title="1" value="1">1</option> </select> <select class="my-select" id="1295454058398"> <option title="1" value="1">1</option> <option title="2" value="2">2</option> </select>
What makes me say: βWait, WHAT ?!β. It seems that when jQuery removes the <option> in the second selection, it removes the same <option> (value = 3) from the first <select> . Not cool.
So what can I do wrong here?
I am testing FireFox 3.5 (same results in IE) using jQuery 1.4.4.
source share