WEFX
WEFX

Reputation: 8562

Using jquery, how do I remove an item from dropdown based ONLY on its text?

When someCondition is true, I'm trying to remove a dropdown option that has "someText" as its Text. I do not know its value. It seems like this should work, but it does not. When debugging, I see that someID is undefined. Does anyone know if I've made some small syntax error?

function toggleSomeOption() {
    if (someCondition() == "Foo") {
        var someID = $("#myDropDown option[text='someText']").attr('value');
        $("#myDropDown option[value='someID']").remove();
    }
}

I've styled my code after this answer, but it won't work.

Upvotes: 3

Views: 2229

Answers (1)

Chandu
Chandu

Reputation: 82923

Try this:

$("#myDropDown option").filter(function(){
    var $this = $(this);
    return $this.text() == "SomeText";
}).remove();

Upvotes: 6

Related Questions