4

Suppose in jQuery I push DOM elements into an array,

var elemarray = [];

elemarray.push($('#elem1'));
elemarray.push($('#elem2'));

Would it be possible to then use $.inArray to determine if the array contains an element?

if ( $.inArray($('#elem2'), elemarray) > -1 ) { .. }

The only examples I saw for primitive types, strings and numbers.

JSFiddle, not working: https://jsfiddle.net/5knyrcph/2/

5
  • 1
    You should really use !== -1 Commented Oct 16, 2017 at 20:46
  • 1
    No, it would not. Each call to $() returns a distinct new jQuery object. Commented Oct 16, 2017 at 20:46
  • 1
    jsfiddle.net/5knyrcph/3 - working fiddle. You have to get the text of the div before comparing. I also utilized !== -1 Commented Oct 16, 2017 at 20:48
  • So you found a way to stringify a field, in this case based on its content. I see Commented Oct 16, 2017 at 20:49
  • Yes for the most part... you have to get the DIV text otherwise it is just an object. Commented Oct 16, 2017 at 20:50

3 Answers 3

3

The problem is that you are not storing DOM elements, but jQuery wrappers. Every time you create such a wrapper, it is a new jQuery object.

Instead use the real DOM elements themselves, which with jQuery you can get with .get():

var elemarray = [];
elemarray.push($('#elem1').get(0));
elemarray.push($('#elem2').get(0));

if ($.inArray($('#elem1').get(0), elemarray) > -1) {
  console.log('Found');
}
else {
  console.log('Not found');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="elem1">
Test
</div>

<div id="elem2">
Test2
</div>

In HTML5 / ES6 you can do this without jQuery in shorter code:

const elemSet = new Set([elem1, elem2]);

if (elemSet.has(elem1)) {
  console.log('Found');
}
else {
  console.log('Not found');
}
<div id="elem1">
Test
</div>

<div id="elem2">
Test2
</div>

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

Comments

0

You really should use !== -1

And here is the fiddle to the corrected JS: https://jsfiddle.net/5knyrcph/3/

Code:

var elemarray = [];

elemarray.push($('#elem1').text());
elemarray.push($('#elem2').text());
alert(elemarray[0]);

if ($.inArray($('#elem1').text(), elemarray) !== -1) {
  alert('Found');
}
else {
  alert('Not found');
}

5 Comments

In other words serialization/stringification of some kind. Thanks for the tip
div is an element, and .text() gives you the text inside the div which you are inserting into the array to compare.
Please accept the answer if this solved your question.
There is no problem in comparing like OP did: > -1 is fine. Secondly, if an element is looked for in the array, that happens to have the same text, but is a different DOM element, this will say "Found", while it is not really the same DOM element.
While it is true, i gave him a easier solution but getting with .get also works in this case, sure.
0

Jquery return an Array of items that match the criteria, so when ever you select an element it always will construct a brand new array of objects that match your criteria, in order to compare the elements you need to either get the element inside the array with these two options:

$(".element")[position]; //or
$(".element").get(position);

If in your fiddle instead of pushing the Jquery object, you push the actual element, then you'll get your results right. Fiddle Updated

Hope this help

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.