Hamed mayahian
Hamed mayahian

Reputation: 2323

Active class not set to `li` with specific index()?

I have a UL LI menu some li have ul children.

Each time root li clicked i add .active to them and then save a li index() to cookie,

The problem is after I save the index() to cookie, the active class set to children index() for example myclicked root li is 7.

But the css go to the children of li 6 and active class add to first li child from index 6, but i want find root li with index 7.

My code is:

$(function () {

        var foo = $.cookie("test");

        if (foo == null) {
            foo = 0;
        }

        $("#MenuArchiv").find("li").removeClass("activ");

        $("#MenuArchiv li").eq(foo).addClass("activ");


        $("#MenuArchiv li").click(function () {
            foo = $(this).index();
            $("#MenuArchiv").find("li").removeClass("activ");
             $(this).addClass("activ");
             $.cookie("test", foo, { expires: 10 });
        });
});

hope to understand my mean :)

Upvotes: 0

Views: 124

Answers (1)

Ria Weyprecht
Ria Weyprecht

Reputation: 1275

That's because the way you use index() gives you the index() of that element in it's parent. But you use eq() on an object that contains all li (even the ones inside other li-elements. So you have two totally different arrays that you want to use the same index on.

change your line with the index()-call to the following and it should work:

foo = $("#MenuArchiv li").index(this);

this makes the same as you do with eq(), it asks for the index of that element in the object of all li-elements

Upvotes: 1

Related Questions