is there any reason this chain does not work? It does not add the class:
document.getElementsByTagName('nav')[0].firstChild.className = "current"
It should return the first child of the nav element which is an <a> which does not happen.
Thanks for your help!
That’s because you have text nodes between
navanda. You can filter them bynodeType:It may seem strange but, for example, if you have the following markup:
Here’s a DEMO.
Why does this happen? Because some browsers may interpret the space between
<nav>and<a>as an extra text node. Thus,firstChildwill no longer work since it’ll return the text node instead.If you had the following markup, it’d work: