user934902
user934902

Reputation: 1204

Apply class to selected LI and ALL previous LIs

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

http://jsfiddle.net/ym5wr/

jQuery

$('li').click(function() {
    $('li').css({ "background-color":"#fff" });
    $(this).prev().andSelf().css({ "background-color":"#ccc" });
});

Upvotes: 0

Views: 73

Answers (1)

Venkata Krishna
Venkata Krishna

Reputation: 15112

JSFIDDLE DEMO

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

Related Questions