Ashkan Mobayen Khiabani Ashkan Mobayen Khiabani - 1 year ago 74
jQuery Question

How to know if two jquery objects are pointing to the same element

The code below alerts


var a = $('html');
var b = $('html');

Is there a way to know if two variables are pointing to the same element?

Answer Source

Using the normal equality operators (ie. == and ===) doesn't work for objects. However, you can use the is() method to compare two jQuery objects, like this:

var $a = $('html'); 
var $b = $('html'); 

if ($$b)) {
} else {
  console.log('not the same');
<script src=""></script>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download