How do you make a li tag active?

How do you make a li tag active?

  1. Try $(‘ul’).on(‘click’, ‘a’, function () { $(‘.active’).removeClass(‘active’); $(this).closest(‘a’).addClass(‘active’); return false; }); Check Demo – Tushar Oct 12 ’15 at 6:40.
  2. @Tushar:Sorry but i cant change my function. – Learning-Overthinker-Confused Oct 12 ’15 at 6:44.
  3. Then repeat the code.

How to add active class to li on click?

$(“#navMenus”). on(‘click’,’li’,function(){ $(this). addClass(“active”); // adding active class }); The above code will add the active class to the li tag whenever the user clicks on the Li element.

How to add active class in li using jQuery?

5 Answers

  1. jQuery var selector = ‘.nav li’; $(selector). on(‘click’, function(){ $(selector).
  2. Pure Javascript: var selector, elems, makeActive; selector = ‘.nav li’; elems = document. querySelectorAll(selector); makeActive = function () { for (var i = 0; i < elems.
  3. jQuery with event delegation:

How to set active class in javascript?

With pure Javascript, you can use Element. classList. add() to add a class to an element.

How to add active class on click event in custom list group?

How to add active class on click event in custom list group in Bootstrap 4 ? – GeeksforGeeks How to add active class on click event in custom list group in Bootstrap 4 ? In Bootstrap 4, Javascript or jQuery events are used to add active class on click event in custom list group.

How to add active class on click event in Bootstrap 4?

In Bootstrap 4, Javascript or jQuery events are used to add active class on click event in custom list group.

How to add active class to current element?

Active Element 1 Step 1) Add HTML: Example 1 2… 2 Step 2) Add CSS: Example / 3 Style the buttons 4 / .btn { border: none; outline: none; padding: 10px… 5 Step 3) Add JavaScript: More

How to remove an active class from an element?

Use document.querySelectorAll to find the element which currently have the active class, then you can remove that class. Instead of document.querySelectorAll you can also use document.querySelector Instead of iterating through the entire collection you can select the element which have a class active using document.queryselector.