How can I check whether a option already exist in select by JQuery

ID : 20383

viewed : 13

Tags : jqueryjquery

Top 5 Answer for How can I check whether a option already exist in select by JQuery

vote vote

93

This evaluates to true if it already exists:

$("#yourSelect option[value='yourValue']").length > 0; 
vote vote

90

Another way using jQuery:

var exists = false;  $('#yourSelect  option').each(function(){   if (this.value == yourValue) {     exists = true;   } }); 
vote vote

74

if ( $("#your_select_id option[value=<enter_value_here>]").length == 0 ){   alert("option doesn't exist!"); } 
vote vote

70

var exists = $("#yourSelect option")                .filter(function (i, o) { return o.value === yourValue; })                .length > 0; 

This has the advantage of automatically escaping the value for you, which makes random quotes in the text much easier to deal with.

vote vote

54

Although most of other answers worked for me I used .find():

if ($("#yourSelect").find('option[value="value"]').length === 0){     ... } 

Top 3 video Explaining How can I check whether a option already exist in select by JQuery

Related QUESTION?