0

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>
artkoshelev
  • 872
  • 7
  • 22
Francesco G.
  • 679
  • 3
  • 13
  • 32
  • Possible duplicate of [jQuery.inArray(), how to use it right?](https://stackoverflow.com/questions/18867599/jquery-inarray-how-to-use-it-right) – Hassan Imam Nov 24 '17 at 17:02

1 Answers1

1

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>