I totally wonder why this code doesn’t work:
$(document).ready(function() {
$('ol ol').hide();
$('li').click(function() {
$(this).children('ol').toggle();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ol>
<li>Coffee</li>
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
<li>Tea</li>
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
<li>Milk</li>
<li>Cool</li>
</ol>
If you click for example "Coffee", then the subordinate list should fold out. Is there a little mistake? Unfortunately, I can’t find it.
4
Answers
Your
ol
s aren’t children of theli
you’re clicking on – you need to change the markup to:Because your
ol
is not the children ofli
.Or you can change your js, use
next('ol')
to selectol
behind theli
you click.You need to put
ol
insideli
Try this code to open a at time one li ol.