jQuery: Identifying selectors, that represent the same DOM node.

If you have two jQuery selectors, that each represent a single DOM node,
and you need to know whether it is the same DOM node, you can easily do this with:

var selector1 = $('#some_item');
var selector2 = $('#some_item');
if (selector1[0] == selector2[0])
{
    // Do stuff if the selectors represent the same DOM node
}

This compares the inner DOM nodes (NOT the jQuery wrapper objects) and there cannot be two different objects representing the same DOM node in memory at the same time, thats why this works. Whereas there can be two different jQuery wrappers for the same DOM node, thats why if (selector1 == selector2) will work only some times.

What [0] at the end of the selector does, is it fetches the inner DOM node, that the jQuery object wraps around. There is another way to retrieve it from the jQuery object: selector1.get(0)

This works across all major browsers (Firefox, Chrome, Opera, IE)

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 3.67 out of 5)
Loading...

3 responses to “jQuery: Identifying selectors, that represent the same DOM node.”

  1. Highest Rate Drones

    About to purchase my first drone in weeks, trying on ebay and putting in work searching and re-looking out.

  2. Lonnie

    Gamblers Anonymous is an impartial help group consisting of men and women who assist other
    get better from problems with enjoying on-line for
    real cash.

  3. What is Linux? What is Technology? - MonkosBlogs.info

    Great write up, got the information I was looking for..

    Thanks

    Monko’s Blogs

Leave a Reply