I’m getting this error: Uncaught TypeError: Cannot call method 'match' of undefined in my JavaScript. I’m trying to write this without jQuery since it’ll be the only js on the site.
My code is supposed to add a class, “active”, to the <a href="...> in the navigation that links to the current page.
I’m guessing it might be the contentLoaded function?….source
Here’s my code…(error occurs on line 9)…fiddle
(function(window, document, undefined) { contentLoaded(window, function() {
var page = document.location.pathname.match(/[A-z]+$/)[0],
nav_anchors = document.getElementsByTagName('header')[0]
.getElementsByTagName('nav')[0]
.getElementsByTagName('a');
for(i in nav_anchors)
if(nav_anchors[i].href.match(/[A-z]+$/)[0] = page) //LINE 9 <-- Error
nav_anchors[i].classList.add('active');
})
})(this, this.document)
Thanks!
NodeListshavelength,itemandnamedItemproperties.Use
for (var i = 0; i < foo.length; i++)notfor i in footo iterate over them and only hit the nodes.