Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
60
rated 0 times [  63] [ 3]  / answers: 1 / hits: 41633  / 13 Years ago, sun, october 9, 2011, 12:00:00

I need to pass an element to a function and then match that specific element while traversing parent. The catch (for someone clueless like me) is that this element doesn't have an id. In the following example, I want every element to turn pink except the one clicked on that should turn yellow



function colorize(element) {
element.parent().find('span').each(function() {
if ($(this)===element) { // the problem is this is always false
$(this).css('background','yellow');
} else {
$(this).css('background','pink');
}
});
}
$('span').click(function() {
colorize($(this));
});

More From » jquery

 Answers
-5

Comparing JQuery objects will never return true, because each JQuery object is a a new object, even if their selectors are equal.



To compare elements, you have to check whether the DOM elements are equal:



this === element.get(0);

[#89716] Friday, October 7, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elliem

Total Points: 415
Total Questions: 117
Total Answers: 94

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago
;