How to check for DOM equality with jQuery? How to check for DOM equality with jQuery? jquery jquery

How to check for DOM equality with jQuery?


Although you can compare jquery objects as Matt has shown, or simply with index operator

if($("#list p.selected")[0] == $("#list p:last-child")[0])...

this is rarely needed with jQuery. There is a better way!

if($("#list p.selected").is(":last-child"))....

or the other way round, more readable

if($("#list p:last-child").is(".selected"))....


jQuery is an object, so you're technically comparing two different (but similar) objects, which will always be false.

Try:

if (jQuery('#list p.selected').get(0) == jQuery('#list p:last-child').get(0))

jQuery.get()