I am kind of confused why my code doesn\'t work correctly, I hope You will tell me what I\'ve done wrong. I want to highlight navigation tab while clicked.
HTML:
You can simplify your JavaScript to:
function dodajAktywne(elem) {
// get all 'a' elements
var a = document.getElementsByTagName('a');
// loop through all 'a' elements
for (i = 0; i < a.length; i++) {
// Remove the class 'active' if it exists
a[i].classList.remove('active')
}
// add 'active' classs to the element that was clicked
elem.classList.add('active');
}
If you pass the parameter this
in your HTML to:
Note: I've changed href
attribute to #
, you will have to change it back to your .html
pages
Alternatively, you can do this without JavaScript, using CSS's :focus
:
a:focus {
color: blue;
background-color: #cf5c3f;
}