How can I check if the var element
exists or not in the array sites
?
var sites = array['test','about','try'];
var element = 'other';
How can I check if the var element
exists or not in the array sites
?
var sites = array['test','about','try'];
var element = 'other';
You can use inArray:
Search for a specified value within an array and return its index (or -1 if not found).
var sites = ['test','about','try'];
var element = 'other';
if($.inArray(element ,sites ) >= 0){
//exists
}
You can use indexOf()
method like following.
if(sites.indexOf(element) > -1) {
//exist
}
if (jQuery.inArray(jQuery("input:first").val(), ar) != -1)
The inArray method returns -1 if the element wasn't found in the array, so as your bonus answer to how to determine if an element is not in an array, use this :
if(jQuery.inArray(el,arr) == -1){
// the element is not in the array
};