If I do:
$("a").on("click", function(e) {
$(this).parent().find("ul").toggleClass("closed opened");
e.preventDefault();
});
It works but it will target all uls of the parent
so I am doing
$("a").on("click", function(e) {
$(this).parent().next("ul").toggleClass("closed opened");
e.preventDefault();
});
But this doesn't work, I get no errors at all.
If I do the following works on JSFiddle using the html used here:
$("a").on("click", function(e) {
$(this).next("ul").toggleClass("closed opened");
e.preventDefault();
});
It doesn't make sense. I need to target the first <ul>
child of the parent element that I am clicking.
<ul>
<li><a href="">Text</a>
<ul class="closed">
<li><a href="">Two</a>
<ul class="closed">
<li><a href="">Three</a>
<ul class="closed">
<li><a href="">Four</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
.closed {
display: none;
}
.opened {
display: block;
}
JSFiddle with the html used in the example
UPDATE
Thanks to comment due to my real html having a
<span>
element I had to use.nextAll()
Actual html
<ul>
<li>
<a href="">Text</a>
<span></span>
<ul class="closed">
<li>
<a href="">Two</a>
<span></span>
<ul class="closed">
<li>
<a href="">Three</a>
<span></span>
<ul class="closed">
<li><a href="">Four</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
inside the li
– rob.m Nov 28 '16 at 00:13