<div class='topnav' id='myTopnav'>
<div class='dropdown'>
<button class=' dropbtn'>Adobe ▼</button>
<div class='dropdown-content'>
<a href='index'>Photoshop</a>
</div>
</div>
</div>
<script>
jQuery(function($) {
var path = window.location.href;
$('#myTopnav a').each(function() {
if (this.href === path) {
$(this).addClass('active');
$(this).parent().parent('first-child').addClass('active');
}
});
});
</script>
I am not very good at jQuery or even Javascript, but I would like to give the button the same class as the anchor tag when the url the anchor is pointing to is correct.
2
Answers
I’ve not working with JQuery for a long time but here is my suggestion.
You can try this one
First, find the closet
dropdown
after that finddropbtn
For finding
.dropbtn
class as i remember it’s may return all element that have.dropbtn
so if you’re in this case I think you may need to get first.Hope it’s can help
Select your options and add class.