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. Show
asked Mar 27, 2016 at 18:19
As per the comments, I guess you are expecting this:
Rayon 35.6k4 gold badges46 silver badges70 bronze badges answered Mar 27, 2016 at 18:20
13 if you have jquery
Vanilla JavaScript (ES6 where I tested, might work on ES5)
answered Mar 27, 2016 at 18:35
a45ba45b 4793 silver badges19 bronze badges 5 Learn how to add an active class to the current element with JavaScript. Highlight the active/current (pressed) button: Try it Yourself » Active ElementStep 1) Add HTML:Example<div id="myDIV"> Step 2) Add CSS:Example /* Style the buttons */ /* Style the active class (and buttons on mouse-over) */ Step 3) Add JavaScript:Example // Get the container element // Get all buttons with class="btn" inside the container // Loop through the buttons and add the active class to the current/clicked button Try it Yourself » If you do not have an active class set on the button element to start with, use the following code: Example // Get the container element // Get all buttons with class="btn" inside the container // Loop through the buttons and add the active class to the current/clicked button // If there's no active class // Add the active class to the current/clicked button Try it Yourself » How do I add active class to UL Li?5 Answers. jQuery var selector = '.nav li'; $(selector). on('click', function(){ $(selector). ... . Pure Javascript: var selector, elems, makeActive; selector = '.nav li'; elems = document. querySelectorAll(selector); makeActive = function () { for (var i = 0; i < elems. ... . jQuery with event delegation:. How do I add a current class to an active link?jQuery(function($) {. var path = window. location. href;. // because the 'href' property of the DOM element is the absolute path.. $('ul a'). each(function() {. if (this. href === path) {. $(this). addClass('active');. How do you add active class on click in react?To add or remove a class on click in React:. Set the onClick prop on the element.. Access the DOM element as event. currentTarget .. Use the classList. add() or classList. remove() methods.. How do I add active class to a navigation menu?Use . addClass("active); to add class named active to the link clicked.
|