Francesco G.
Francesco G.

Reputation: 717

jQuery check if exist in array an html element

I would to have an array with html element, but I don't want have duplicate. Is possible that $.inArray() don't work with html element?

var rowsArr= new Array();
var rowMst=$('.a');

rowsArr.push(rowMst[0]);

if($.inArray(rowsArr, rowMst[0]) === -1) { 
  rowsArr.push(rowMst[0]); 
}else{
  alert("Already exist");
}

console.dir(rowsArr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="a">Hello</div>
<div class="b">Stack</div>
<div class="c">Overflow</div>

Upvotes: 0

Views: 50

Answers (1)

VIJAYABAL DHANAPAL
VIJAYABAL DHANAPAL

Reputation: 564

Yes,inArray will work. Minor issue in your code at line number 4 corrected here. $.inArray(value, array, index)

var rowsArr= new Array();
var rowMst=$('.a');

rowsArr.push(rowMst[0]);

if($.inArray(rowMst[0],rowsArr) === -1) { 
  rowsArr.push(rowMst[0]); 
}else{
  alert("Already exist");
}

console.dir(rowsArr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="a">Hello</div>
<div class="b">Stack</div>
<div class="c">Overflow</div>

Upvotes: 1

Related Questions