0
<ul class="nav nav-tabs">
    <li onclick="this.className='active'"><a href="#">Home</a></li>
    <li onclick="this.className='active'"><a href="#">Menu 1</a></li>
    <li onclick="this.className='active'"><a href="#">Menu 2</a></li>
    <li onclick="this.className='active'"><a href="#">Menu 3</a></li>
</ul>

How can I add active class on li tag with JavaScript. Here I am try to do it. It is working but not properly. I am doing for tabs here.

2 Answers 2

3

As per the comments, I guess you are expecting this:

var a = document.querySelectorAll(".nav li a");
for (var i = 0, length = a.length; i < length; i++) {
  a[i].onclick = function() {
    var b = document.querySelector(".nav li.active");
    if (b) b.classList.remove("active");
    this.parentNode.classList.add('active');
  };
}
.active {
  background-color: #0f9;
}
<ul class="nav nav-tabs">
  <li><a href="#">Home</a>
  </li>
  <li><a href="#">Menu 1</a>
  </li>
  <li><a href="#">Menu 2</a>
  </li>
  <li><a href="#">Menu 3</a>
  </li>
</ul>

Sign up to request clarification or add additional context in comments.

13 Comments

how can I remove class with other li
@user3282537 For that you have to write a dedicated event listener. You can't do all the things here in an inline handler.
@RajaprabhuAravindasamy Added Event handler buddy.
@PraveenKumar, It happens mate! querySelector would look better as you will not need [0] with that..And keep it in variable instead of selecting element twice..
@PraveenKumar, Yes... Now it is better and will have less side effects.. :)
|
1

if you have jquery

<ul class="nav nav-tabs">
  <li><a href="#">Home</a></li>
  <li><a href="#">Menu 1</a></li>
  <li><a href="#">Menu 2</a></li>
  <li><a href="#">Menu 3</a></li>
</ul>

<style>
 .active {background-color: #0f9;}
</style>
<script type="text/javascript">
$("ul.nav.nav-tabs").on('click', 'li', function(){
  $(this).siblings().removeClass('active');
  $(this).addClass('active');
});
</script>

Vanilla JavaScript (ES6 where I tested, might work on ES5)

const elm = document.querySelector('ul');
elm.addEventListener('click', (el) => {
  const elActive = elm.querySelector('.active');
  if (elActive) {
    elActive.removeAttribute('class');
  }
  el.target.setAttribute('class', 'active');
});

4 Comments

No. Look here.
😊 I updated with Vanilla JavaScript. But I did mention if you have jquery and in my defense, I will say jQuery is JavaScript (it's a javascript library)
Wow, why such a long time later, updating this? Anyway, I'll give you a vote up if you feel good. 😁
Thanks, @PraveenKumarPurushothaman :-) I actually got a notification and when click, I end up here (not sure why 🧐). I saw your comment and updated my answer. I hope it helps...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.