I know JQuery make our lives much easier, but I’m trying to write some lines in vanilla javascript…so here it goes:
$('.menu a').filter(function() {
return this.href == url;
}).closest('li').addClass('active');
Many thanks!
I know JQuery make our lives much easier, but I’m trying to write some lines in vanilla javascript…so here it goes:
$('.menu a').filter(function() {
return this.href == url;
}).closest('li').addClass('active');
Many thanks!
2
Answers
document.querySelectorAll('.menu a')
– gets all thea
elements inside.menu
In Chrome 105+ you can use the :has pseudo selector:
Considering you’re matching the
href
attribute: