Add active class onclick javascript and jQuery

Add active class to a div on javascript onclick event

Here we are applying a style to a simple div.

Steps to add a css class on click event

  1. Create a div with specific id (here id=”text”)
  2. on click on div call a method makeActive()
  3. Define a function makeActive() in side <script> tag.
  4. Inside makeActive() getElementById
  5. Get Elements classList and add class active.
  6. Use the innerHTML to change to element Text


    
        
    
        
Click to make Active

Add active class onclick javascript to HTML ul List to make menu

Here We have ul list elements to create horizontal menu on click on any element an active class is applied on it and if any one have already active class then remove it.

  1. create a ul menu with li items
  2. make menu display inline using css
  3. inside script tag select all anchor in menu class
  4. on click event find is there any active class in li
  5. if found then remove it
  6. add active class to clicked element.


    
        
    
         


Reference

Add active class to a div on Jquery onclick event

Here we have taken example of only a single div on click on div it will add an active class on div.

you can download or include Jquery from CDN

To achieve this follow these steps

  1. Define active css class
  2. Include Latest Jquery jquery.min.js file to your html
  3. Create a div with id text.
  4. Write Jquery code inside script tag.
  5. Check for document ready state if ready then
  6. write click function for #text
  7. add a class using jquery addClass()
  8. Change the div content using Jquery text().


    
        
    
        
Click to make Active

Add active class onclick JQuery to HTML ul List to make menu

As we already seen how to add a active class in HTML List.

The same work we will do with JQuery.

JQuery provides easy way to add and remove css class in any element

  1. Create a simple HTML unordered list
  2. Style list to make inline and provide margin
  3. define a active style in css
  4. Include Jquery latest jquery.min.js file
  5. Write Jquery event that will work on document ready
  6. On click on anchor tag inside li first remove all active class from li then add active class in parent li of anchor tag.


    
        
    
         



Jquery provides addClass() and removeClass() to add and remove any css class.

You can learn how to access parent class id in JQuery

Add Hover effect in HTML menu using JQuery hover()



    
        
    
         

To add hover effect we used below code.

This will add and remove css class on hover.

 
$( "a" ).hover(function() {
                  $(this).parent("li").addClass("hover");
              }, function() {
                  $(".menu li").removeClass("hover");
               });

Read More

  1. Get multiple checkbox value in javascript
  2. JavaScript Multiplication Table
  3. isPrime() Javascript : Prime number program
  4. Form Handling and Validation
  5. Try Catch and Finally in JavaScript
  6. Math Object in JavaScript