I want to select the set of <li>
elements that come before the one with the "active" class and not that are come after "active" class.
<li></li>
<li></li>
<li></li>
<li></li>
<li class="active"></li>
<li></li>
<li></li>
<li></li>
<li></li>
I want to select the set of <li>
elements that come before the one with the "active" class and not that are come after "active" class.
<li></li>
<li></li>
<li></li>
<li></li>
<li class="active"></li>
<li></li>
<li></li>
<li></li>
<li></li>
2
Answers
Use
querySelectorAll()
to select all the<li>
elements into a list. Thenfilter()
that list, searching for the one which is active. Once you find it, set a flag and ignore the rest of the<li>
elements.Here’s how you can select all the elements above the class
active
in CSS and JSFOR your css:
For your JavaScript: