user2945241 user2945241 - 1 year ago 98
jQuery Question

jQuery indexOf is not working properly

I ran in to this problem, and can't figure out what to do.

I have table:

<table class="jtable">

And I need to check first column, this is what I do:

var arr = new Array(2, 1);
$('.jtable').find("tbody tr").each(function(){
var firstCol = $(this).find("td:first").text();
var inArray = arr.indexOf(firstCol);
alert(firstCol+" "+inArray);
if(inArray == -1){
$(this).css("background", "red");
} else {
$(this).css("background", "green");

But all I get is -1 for all loop cycles from
, when I change
to one of the numbers in array it works good, but I need dynamic var there. Any Solutions to this, or what I am doing wrong?

Here is the fiddle:

I tried this with

Answer Source

You are looking for a string in an integer array. You should convert to an int before indexOf

 var inArray = arr.indexOf(parseInt(firstCol));


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download