What you actually need here is .each()
, instead of .filter()
:
var drop2 = $("select[id=dropdown] option"); // the collection of initial options
$("select[id=test]").change(function () {
var drop1selected = parseInt(this.value); //get drop1 's selected value
$("select[id=dropdown]")
.html(drop2) //reset dropdown list
.find('option').each(function () {
if (parseInt(this.value) === drop1selected)
{
$(this).remove();
};
});
});
As .filter()
will remove the element from the result set of matching elements, but it will not remove them from the DOM. You may want to use it like this:
var drop2 = $("select[id=dropdown] option"); // the collection of initial options
$("select[id=test]").change(function () {
var drop1selected = parseInt(this.value); //get drop1 's selected value
$("select[id=dropdown]")
.html(drop2) //reset dropdown list
.find('option').filter(function () {
return parseInt(this.value) === drop1selected;
}).remove();
});