I needed help on how to hide the second level ul & li and only display it if the 1st level li is clicked. For some reason, I can not find the document to add an onclick() function on the html part. Is this possible? Looking for js and css to get it work. Thanks in advance.
So, by default, the list should look like this:
Abgasanlage
Steuerkettenantrieb
When Abgasanlage is clicked, its subcategory will open which should look like this:
Abgasanlage
- Sensoren
Steuerkettenantrieb
When Steuerkettenantrieb is clicked, its subcategory will open which should look like this:
Abgasanlage
Steuerkettenantrieb
-
Steuerkettensätze
-
Steuerketten
-
Gleitschienen
-
Kettendeckel
-
Ritzel
Here’s the html code:
<ul class="sidebar_cate">
<li class="grid__item lvl-1 active">
<a href="/collections/abgasanlage" class="site-nav lvl-1">Abgasanlage</a>
<ul class="subLinks">
<li class="lvl-2">
<a href="/collections/sensoren" class="site-nav lvl-2">Sensoren</a>
</li>
</ul>
</li>
<li class="grid__item lvl-1 ">
<a href="/collections/steuerket-tenantrieb" class="site-nav lvl-1">Steuerkettenantrieb</a>
<ul class="subLinks">
<li class="lvl-2">
<a href="/collections/steuerkettensatze" class="site-nav lvl-2">Steuerkettensätze</a></li>
<li class="lvl-2">
<a href="/collections/steuerketten" class="site-nav lvl-2">Steuerketten</a></li>
<li class="lvl-2">
<a href="/collections/gleitschienen" class="site-nav lvl-2">Gleitschienen</a></li>
<li class="lvl-2">
<a href="/collections/kettendeckel-und-kettenrader-ritzel" class="site-nav lvl-2">Kettendeckel</a></li>
<li class="lvl-2">
<a href="/collections/ritzel" class="site-nav lvl-2">Ritzel</a></li>
</ul>
</li>
</ul>
2
Answers
i think it can be resolved in your js file using these lines of code in a function that get the selected super item id as param
This can be done together with Js and CSS.
First give a class
subLinks
to inner UL. then hide it with CSS.then write a click event on each li, check if inner
sublinks
exist then addactive
class to theli
element. based on theactive
class show the inner list with css.JS
CSS
see the example I have created. https://stackblitz.com/edit/nested-ul-li-list-toggle-basic
This is a basic example to show how you can achieve it, you can further customize it to your needs and add more check to prevent clicking on the link etc.