I am currently having troubles getting into my list subclass. The idea is to create multi-level unfolding list with check boxes working as buttons. Right now it all folds/unfolds at the same time and we want elements to unfold by checking/unchecking it's parent.
I do realize I can use less/sass to hide/show those elements by clicking on them, but I would like to learn how do do it with jQuery.
$(document).ready(() => {
$('.sub-ul').hide();
$('.side-checkbox').each(function () {
var obj = $('.sub-ul');
$(this).click(function () {
if ($(this).is(':checked')) {
obj.show(300);
} else {
obj.hide(200);
}
});
});
});
a {
text-decoration: none;
color: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="ul-nav">
<li class="li-nav">
<a class="side-menu-checkbox" href="#">(A)Lorem</a>
<input type="checkbox" name="a" id="a" class="side-checkbox">
<ul class="ul-nav sub-ul">
<li class="li-nav">
<a href="#">(B)Ipsum</a>
<input type="checkbox" name="a" id="b" class="side-checkbox">
<ul class="ul-list sub-ul">
<li class="li-nav"><a href="#">(C)Dolorem</a></li>
<li class="li-nav"><a href="#">(C)Dolorem</a></li>
<li class="li-nav"><a href="#">(C)Dolorem</a></li>
</ul>
</li>
<li>
<a href="#" class="side-menu-plus">(B)Ipsum</a>
<input type="checkbox" name="a" id="c" class="side-checkbox">
<ul class="ul-list sub-ul">
<li class="li-nav"><a href="#">(C)Dolorem</a></li>
<li class="li-nav"><a href="#">(C)Dolorem</a></li>
<li class="li-nav"><a href="#">(C)Dolorem</a></li>
</ul>
</li>
</ul>
</li>
<li class="li-nav"><a href="#">(A)Lorem</a></li>
<li class="li-nav">
<a class="side-menu-checkbox" href="#">(A)Lorem</a>
<input type="checkbox" name="a" id="a" class="side-checkbox">
<ul class="ul-nav sub-ul">
<li class="li-nav">
<a href="#">(B)Ipsum</a>
<input type="checkbox" name="a" id="b" class="side-checkbox">
<ul class="ul-list sub-ul">
<li class="li-nav"><a href="#">(C)Dolorem</a></li>
<li class="li-nav"><a href="#">(C)Dolorem</a></li>
<li class="li-nav"><a href="#">(C)Dolorem</a></li>
</ul>
</li>
<li>
<a href="#" class="side-menu-plus">(B)Ipsum</a>
<input type="checkbox" name="a" id="c" class="side-checkbox">
<ul class="ul-list sub-ul">
<li class="li-nav"><a href="#">(C)Dolorem</a></li>
<li class="li-nav"><a href="#">(C)Dolorem</a></li>
<li class="li-nav"><a href="#">(C)Dolorem</a></li>
</ul>
</li>
</ul>
</li>
<li class="li-nav"><a href="#">(A)Lorem</a></li>
</ul>
element.
Aucun commentaire:
Enregistrer un commentaire