Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ensure two items 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 false:

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

even though:

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

Could anyone explain why? Thanks!

like image 462
abrad45 Avatar asked Feb 20 '12 14:02

abrad45


1 Answers

I'm not 100% on exactly why it doesn't work, but I believe it is because the elements are wrapped in jQuery objects which are inherently different per element.

As a workaround, you can compare the native DOM object, like this:

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

Example fiddle

like image 199
Rory McCrossan Avatar answered Oct 09 '22 16:10

Rory McCrossan