Reputation: 94
Can someone explain why this works:
$(function(){
$("ol li a").click(function(){
$("ol li a").filter(":even").css("color", "orange");
});
});
But this doesn't:
$(function(){
$("ol li a").click(function(){
$(this).filter(":even").css("color", "orange");
});
});
I thought using $(this) in this context would refer to the jquery object which has my original selector in it.
Upvotes: 0
Views: 81
Reputation: 4584
When you click the button, the event will fire off and the $(this)
is created, however, $(this)
is ONLY the clicked element, not the entire set of elements, therefore it won't be able to filter since it's only one element.
Upvotes: 2
Reputation: 60075
this
is not collection of the elements that correspond to original selector, it is particular element that received the event. If you have selector that returns single element than $(this)
and $(selector)
are interchangeable, but not in your case.
Upvotes: 3
Reputation: 68440
Because $(this)
is a single element (the target of the event) while $("ol li a")
constains 0 to N elements matching that sector.
Upvotes: 5