Skip to content Skip to sidebar Skip to footer

How To Toggle Next Ul Class?

I am trying to open/close nested sub menus, I have three or four levels of nested uls which are all closed but the first one. When I start clicking on the elements, the next ul sho

Solution 1:

You should use $(this).next("ul") instead of $(this).parent().next("ul"), because ul is the immediate next element of the button. And you can use toggleClass method for toggling class like following.

$("button").on("click", function(){
    $(this).next("ul").toggleClass("closed opened");
});

Post a Comment for "How To Toggle Next Ul Class?"