Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if two jQuery selectors have selected the same element

Is there a way for me to progamatically determine if two jQuery selectors have selected the same exact element? I am trying to loop over a set of divs and skip one of them. What I would like is something like this:

var $rows, $row, $row_to_skip;
$rows = $('.row-class')
$row_to_skip = $('#skipped_row')

$.each($rows, function (id, row) {
        $row = $(row);
        if (!$row == $row_to_skip) {
            // Do some stuff here.
        };
    });
like image 359
Michael Davis Avatar asked Jun 20 '13 21:06

Michael Davis


People also ask

How can I check if two values are equal in jQuery?

Approach 2: The == operator is used to compare two JavaScript elements. If both elements are equal then it returns True otherwise returns False.

How can you tell if two elements are the same?

Two different elements have similar chemical properties when they have the same number of valence electrons in their outermost energy level. Elements in the same column of the Periodic Table have similar chemical properties.

Can we use multiple selectors in jQuery?

You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order.


2 Answers

You can pass jQuery objects into .not():

$rows.not($row_to_skip).each(function() {
    ...
});
like image 187
Blender Avatar answered Sep 27 '22 21:09

Blender


You can use .is()

if (!$row.is($row_to_skip)) {
            // Do some stuff here.
   };
like image 34
Mohammad Adil Avatar answered Sep 27 '22 20:09

Mohammad Adil