Reputation: 1204
When I select an LI I want to apply a class to the selected LI and ALL previous LIS, right now I only have it working for the selected LI and one previous LI any help to select all previous
FIDDLE
jQuery
$('li').click(function() {
$('li').css({ "background-color":"#fff" });
$(this).prev().andSelf().css({ "background-color":"#ccc" });
});
Upvotes: 0
Views: 73
Reputation: 15112
Use .prevAll() instead of .prev()
$(this).prevAll().andSelf().css({ "background-color":"#ccc" });
Or, since andSelf()
is deprecated in version 1.8, use addBack()
$(this).prevAll().addBack().css({ "background-color":"#ccc" });
Upvotes: 4