JS jQuery - check if value is in array JS jQuery - check if value is in array arrays arrays

JS jQuery - check if value is in array


You are comparing a jQuery object (jQuery('input:first')) to strings (the elements of the array).
Change the code in order to compare the input's value (wich is a string) to the array elements:

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};


As to your bonus question, try if (jQuery.inArray(jQuery("input:first").val(), ar) < 0)


Alternate solution of the values check

//Duplicate Title Entry     $.each(ar , function (i, val) {        if ( jQuery("input:first").val()== val) alert('VALUE FOUND'+Valuecheck);  });