...">

Make sure the two elements are siblings in JS / jQuery

Given the following HTML structure:

<div class="wrap">
    <div id="a"></div>
    <div id="b"></div>
</div>

the following is not true:

($('#a').parent() == $('#b').parent()); //=> false

though:

$('#a').parent().children('#b').length; //=> 1

Can anyone explain why? Thank!

+5
source share
2 answers

I am not 100% sure why this does not work, but I believe that this is because the elements are wrapped in jQuery objects that are essentially different from each other.

As a workaround, you can compare your own DOM object, for example:

($('#a').parent()[0] == $('#b').parent()[0]); // true

Script example

+6
source

For the same reason that $('#a) == $('#a')there isfalse

, jQuery , ( jQuery , ). JavaScript, , , ;

var a = {
    foo: 1
};
var b = {
    foo: 1
};

(a == b) // false;

, DOM ( .get(i), jQuery, ([i])), is();

if ($('.foo').get(i) == $('.bar').get(i));
if ($('.foo')[0] == $('.bar')[0]);
if ($('.foo').is($('.bar')); // or even...
if ($('.foo').is('.bar')); 
+3

All Articles