user1711245
user1711245

Reputation:

Jquery is not adding class

But if I try adding anything other than the Selected, it's working.**

I updated the code to

But still the same.

Upvotes: 1

Views: 111

Answers (3)

user1711245
user1711245

Reputation:

At last I just able to understand the glich. when subcatlist li element is clicked (which is on catlist) fires that catlist li is clicked. so, first it removes all the selected class for every li element within the catlist (including the subcatlist li).

Now, on the first function selected class is added to the catlist li. so catlist li item remains ok. but li item in subcatlist don't have the selected class. best way to get around this i think is to use a different class.

Upvotes: 0

Anusha
Anusha

Reputation: 281

Try changing $(".CatList .Selected .SubCatList li").click(function () { to $(".CatList ul.SubCatList li").click(function () {

Upvotes: 0

MarcoK
MarcoK

Reputation: 6110

$(this, ".CatList li")
$(this, ".CatList .Selected .SubCatList li")

Those should be turned around:

$(".CatList li", this)
$(".CatList .Selected .SubCatList li", this)

When providing two parameters, you're saying: "Search for this (first param), inside this (second param)".

========= UPDATE

My code above explains when you want to search "within" your selection. But when looking a bit better at your code, I assume you want to add the class to the element you clicked on.

If that's the case, you don't need to subselect, but just provide the $(this) as selector.

$(".CatList li").click(function () {
    $(".CatList li").removeClass("Selected");
    $(this).addClass("Selected");
});

Upvotes: 2

Related Questions