I have a navigation with three links. What I want to do is to show one of three boxes paired on these links if a specific link is clicked. I have these <li> elements with data-nav set:
<li class="active" data-nav="1">Prerender</li><li data-nav="2">Prefetch</li><li data-nav="3">Preconnect</li>
I also have these three boxes, which should be visible only if paired <li> element is clicked
<div class="content-box box1">
<h3 id="isPrerender"> Prerendered page: <span id='pagetitle'></span></h3>
</div>
<div class="content-box box2">
<h3 id="isPrerender"> Page2: <span id='pagetitle'></span></h3>
</div>
</div>
This is what I have in my .css file
.box1, .box2, .box3{display: none;}
.expanded{display: block !important;}
This is where I set the expanded class on the box which is paired with the <li> element clicked:
for (var i = 0; i < menu_items.length; i++) {
menu_items[i].onclick = function() {
var navId = this.getAttribute('data-nav');
document.getElementByClassName('box' + navId).classList.add('expanded');
}
}
}
The problem is that display: none is still set even when I add the expanded class. If I look into console, the display: block rule is here but is erased. How to rewrite It?
document.getElementByClassNameis not a function. It should be getElementsbyClassNameclassListdirectly on it. (and when this is done right, you won't need !important. I'd recommend dropping that now)