Q:

Error check value exist in array when use jquery

Html    
    <table id="gwForm">
       <tbody>
          <tr data-id="1"></tr>
          <tr data-id="2"></tr>
       </tbody>
    </table>

Script
var ids = [];
$("#gwForm tbody tr").each(function(index, el){ 
   ids.push($(el).data('id')); 
});

var id = 3; //=1 or =2
if($.inArray(id, ids) != -1) {
   alert("the element is not in the array");
} else {
   alert("the element in the array");
}

When id=3 (or =2) result is the element is not in the array. How to fix it ?

A:

You have your condition backwards.
if($.inArray(id, ids) != -1) {
   alert("the element is not in the array");
} else {
   alert("the element in the array");
}

Should be
if($.inArray(id, ids) > -1) {
   alert("the element is in the array");
} else {
   alert("the element is not in the array");
}

