Compare jQuery Objects

Avatar of Chris Coyier
Chris Coyier on

You can’t really compare if two jQuery objects are the same…

if ($(selectionOne) === $(selectionTwo)) {

}

You can compare DOM objects though…

if ($(selectionOne)[0] === $(selectionTwo)[0]) {

} 

But that’s only really useful if you’re comparing a single element, not a collection.

If you need to compare a collection of elements, this StackOverflow thread has the answer:

var divs = $("div");
var divs2 = $("div");

if (divs.length == divs2.length && divs.length == divs.filter(divs2).length) {         
  // They are equal
}