How would you compare jQuery objects?

Kyle Hotchkiss picture Kyle Hotchkiss · Mar 13, 2010 · Viewed 77k times · Source

So I'm trying to figure out how to compare two jQuery objects, to see if the parent element is the body of a page.

here's what I have:

if ( $(this).parent() === $('body') ) ...

I know this is wrong, but if anybody understands what I'm getting at, could they point me towards the correct way of doing this?

Answer

Christian C. Salvadó picture Christian C. Salvadó · Mar 13, 2010

You need to compare the raw DOM elements, e.g.:

if ($(this).parent().get(0) === $('body').get(0))

or

if ($(this).parent()[0] === $('body')[0])