-1

when I trying to add a classname into my js code which looks like this

var a = document.querySelectorAll('.nav-tabs');
    for(var i=0 ; i<a.length; i++){
    a[i].addEventListener('click',function(){
        a[i].classList.add("active");
    });
}

it show something like Uncaught TypeError: Cannot set property 'className' of undefined and my html looks like this

<nav>
    <div class="container" id="container">
    <ul class="nav nav-tabs">
         <li role="presentation"><a href="#">a</a></li>
         <li role="presentation"><a href="#">b</a></li>
         <li role="presentation"><a href="#">c</a></li>
    </ul>
    </div>
</nav>

I'll be so thankful if someone could help me solve this problem. Muchas gracis

Daniel Lau
  • 23
  • 1
  • 5

3 Answers3

0
var a = document.querySelectorAll('.nav-tabs');
for(var i=0 ; i<a.length; i++){
    a[i].addEventListener('click',function(){
        this.classList.add("active");
    });
}
Todor Simeonov
  • 806
  • 1
  • 6
  • 11
  • Yeah, you're right. The selector should be `'.nav-tabs li a'` – Todor Simeonov May 15 '17 at 21:02