I am in the process of update some jQuery code to be in plain JS. Currently the code is using jQuery closest method to select the closest UL. I need help in finding the right approach to selecting the closest UL using javascript.
Here is a sample of the HTML code:
<ul class ="m_nav_tier m_nav_tier1">
<li data-nav-tier="1">
<a class="m_drop" href="#">Link1</a>
<ul class="m_nav_tier m_nav_tier2">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</li>
</ul>
What I am trying to do is change the class of <ul class="m_nav_tier m_nav_tier2">
when user clicks on Link 1
Here is what I have tried so far:
var mdrop = document.getElementsByClassName('m_drop');
for (i = 9; i < mdrop.length; i++) {
//Forward
mdrop[i].onclick = function () {
//make tier 1 inactive
var tier1 = document.getElementsByClassName('m_nav_tier1')[1];
tier1.className += " inactive";
//find closest UL and make it active
document.querySelector('ul .m_nav_tier2')[9].className += " active";
};
Sample Fiddle *UPDATE: I was able to change the class with the following code:
document.getElementsByClassName('m_nav_tier2')[9].className += " active";
But the problem is I have to set the array a value. I tried placing [i] but that didn't work. How can I up update the class depending on which LI was selected?