Skip to content Skip to sidebar Skip to footer

How To Check If String Is .inarray

How can I check if a string is contained with my array? Here's how I'm putting the array together... // get the select var $dd = $('#product-variants'); if ($dd.length > 0)

Solution 1:

The other answers are correct so far as use $.inArray() here, however the usage is off, it should be:

if($.inArray('Kelly Green', arrVals) != -1) {
  $("#select-by-color-list li#kelly-green").show();
}

$.inArray() returns the position in the array (which may be 0 if it's first...so it's in there, but that if() would be false). To check if it's present, use != -1, which is what it'll return if the element is not found.

Solution 2:

You can use jQuery's inbuilt .inArray() method:

if($.inArray('Kelly Green', arrVals) != -1) {
    $("#select-by-color-list li#kelly-green").show();
}

Solution 3:

Try this:

console.log($.inArray('Kelly Green', arrVals);

if($.inArray('Kelly Green', arrVals)
{
  $("#select-by-color-list li#kelly-green").show();
}

Possible dupe: Need help regarding jQuery $.inArray()

Post a Comment for "How To Check If String Is .inarray"