javascript - Check existence of values in array on multi select dropdown focusout in jQuery -
i have default array have fixed values showing multiselect dropdown user.so on focusout of drop down want check whether values selected have values in default array.if values missing want alert them user
html
<form action="#" method="post"> <fieldset> <label for="selecteditemlists">select values:</label> <select id="selecteditemlists" name="selecteditemlists" multiple> <option val="value1" selected >value1</option> <option val="value2">value2</option> <option val="value3" selected>value3</option> <option val="value4">value4</option> <option val="value5">value5</option> </select> </fieldset> <fieldset> <input type="submit" value="submit" /> </fieldset> </form>
jquery
var default_values = ["value1","value3"]; $("#selecteditemlists").live('focusout',function(){ var new_selectedvalues = $("#selecteditemlists").val(); //here want compare both arrays , alert him default values missing });
a simple nested $.each
loop it:
//here want compare both arrays , alert him default values missing $.each(default_values, function(_, defaultval){ var found = false; $.each(new_selectedvalues, function(){ if(this == defaultval){ found = true; return false; } }); if(!found){ alert("please select default: " + defaultval); } });
note: .live()
deprecated jquery 1.7, .on
should used instead (unless working old version).
Comments
Post a Comment