Reputation: 2430
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.
};
});
Upvotes: 5
Views: 1094
Reputation: 817228
You can compare the actual DOM element selected by jQuery:
var row_to_skip = $row_to_skip.get(0);
$.each($rows, function (id, row) {
if (row !== row_to_skip) {
// Do some stuff here.
}
});
Two jQuery objects will always be different from each other, even if they select the same elements (just like two empty objects are different).
However in your case, instead of comparing inside the loop, it's cleaner to just remove the element from the set:
$('.row-class').not("#skipped_row").each(function() {
// do stuff
});
Upvotes: 3
Reputation: 298582
You can pass jQuery objects into .not()
:
$rows.not($row_to_skip).each(function() {
...
});
Upvotes: 8
Reputation: 44740
You can use .is()
if (!$row.is($row_to_skip)) {
// Do some stuff here.
};
Upvotes: 4