Add active class onclick javascript
Learn how to add an active class to the current element with JavaScript. Show Highlight the active/current (pressed) button: Try it Yourself » Active ElementStep 1) Add HTML:ExampleStep 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 » I am trying to make a navigation menu I did all the HTML and CSS when come to javascript I am struck in the middle I am able to add a class to the element, but I am not able to remove the class remaining elements. Please help me with this.
and here is my Codepen
asked Aug 17, 2016 at 7:06
3 Use
JSFIDDLE Instead of
JSFIDDLE 2 Edit Instead of iterating through the entire collection you can select the element which have a class
|