I'm new to coding in javascript and I could use some assistance solving an issue on my test website. The problem involves creating a top menu with an active button that stays highlighted when clicked. In this case, I specifically need the active CSS class for buttons 1 and 2. Button 3 is a link to an email address and button 4 is a logout function.
I found one solution for the active class button, but it resulted in a new issue arising.
HTML:
<menu id="nav">
<ul>
<li><a href="index.php" class="btn btn-default"><i class="fa fa-tachometer fa-lg"></i><p class="desc_menu">BTN_1</p></a></li>
<li><a href="index.php?nav=1" class="btn btn-default"><i class="fa fa-cogs fa-lg"></i><p class="desc_menu">BTN_2</p></a></li>
<li><a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c5bdb5b1aa85bdb5b1aaeba6aaa8">[email protected]</a>" class="btn btn-default"><i class="fa fa-envelope-o fa-lg">EMAIL</i><p class="desc_menu">COMUNICATIONS</p></a></li>
<li><a href="exit.php" class="btn btn-default"><i class="fa fa-rocket fa-lg"></i><p class="desc_menu">EXIT</p></a></li>
</ul>
</menu>
JAVASCRIPT:
$(function() {
var pgurl = window.location.href.substr(window.location.href
.lastIndexOf("/")+1);
$("#nav ul li a").each(function(){
if($(this).attr("href") == pgurl || $(this).attr("href") == '' )
$(this).addClass("activ");
})
});
Clicking the green section link causes the active CSS styling on the button in the top menu to disappear. How can I keep the button active?
See example of disappearing active button
Any suggestions on resolving this problem would be greatly appreciated!
Thank you