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>

1

1 Answer 1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.