html - jQuery add class to current li and remove prev li when click inside li a

Html - jQuery add class to current li and remove prev li when click inside li a

You can achieve this using jQuery by adding a click event listener to the <a> elements inside the <li> elements. Within the event handler, you can add a class to the clicked <li> and remove the class from its siblings. Here's an example:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>jQuery Add Class to Current LI</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <style> .active { background-color: yellow; /* Adjust as needed */ } </style> </head> <body> <ul> <li><a href="#">Item 1</a></li> <li><a href="#">Item 2</a></li> <li><a href="#">Item 3</a></li> <li><a href="#">Item 4</a></li> </ul> <script> $(document).ready(function(){ $("ul li a").click(function(){ // Remove the active class from all li elements $("ul li").removeClass("active"); // Add the active class to the parent li of the clicked anchor $(this).parent().addClass("active"); }); }); </script> </body> </html> 

In this example, when you click on any <a> element inside an <li>, it removes the active class from all <li> elements and adds it only to the parent <li> of the clicked <a>. This effectively highlights the clicked <li> while removing the highlight from others.

Examples

  1. jQuery add class to clicked li element and remove class from previously clicked li element

    • Description: This query seeks a jQuery solution to add a class to the currently clicked li element and remove the class from the previously clicked li element when a link (a) inside the li is clicked.
    // Code Implementation $('ul').on('click', 'li', function() { $(this).addClass('active').siblings().removeClass('active'); }); 
  2. jQuery toggle class on click inside nested list

    • Description: This query looks for jQuery code to toggle a class on the clicked li element within a nested list structure.
    // Code Implementation $('ul').on('click', 'li', function() { $(this).toggleClass('active').siblings().removeClass('active'); }); 
  3. How to add class to clicked li element in jQuery?

    • Description: This query addresses adding a class to the clicked li element using jQuery.
    // Code Implementation $('ul').on('click', 'li', function() { $(this).addClass('active').siblings().removeClass('active'); }); 
  4. jQuery add class to current list item and remove from others

    • Description: This query seeks jQuery code to add a class to the current list item and remove it from others when clicked.
    // Code Implementation $('ul').on('click', 'li', function() { $(this).addClass('active').siblings().removeClass('active'); }); 
  5. jQuery click event to add class to li and remove from others

    • Description: This query is about using a jQuery click event to add a class to the clicked li element and remove it from other li elements.
    // Code Implementation $('ul').on('click', 'li', function() { $(this).addClass('active').siblings().removeClass('active'); }); 
  6. jQuery remove class from previous li on click

    • Description: This query seeks jQuery code to remove a class from the previously clicked li element when a new one is clicked.
    // Code Implementation $('ul').on('click', 'li', function() { $(this).addClass('active').siblings().removeClass('active'); }); 
  7. jQuery add class to current li and remove from others when link clicked

    • Description: This query wants to add a class to the current li element and remove it from others when a link inside the li is clicked.
    // Code Implementation $('ul').on('click', 'li a', function(event) { event.preventDefault(); $(this).closest('li').addClass('active').siblings().removeClass('active'); }); 
  8. jQuery switch class on click within list items

    • Description: This query involves switching classes on click within a list of items using jQuery.
    // Code Implementation $('ul').on('click', 'li', function() { $(this).addClass('active').siblings().removeClass('active'); }); 
  9. How to remove class from previous li and add to current li in jQuery?

    • Description: This query addresses removing a class from the previously clicked li and adding it to the currently clicked li using jQuery.
    // Code Implementation $('ul').on('click', 'li', function() { $(this).addClass('active').siblings().removeClass('active'); }); 
  10. jQuery add class to current li on click

    • Description: This query is about adding a class to the currently clicked li element using jQuery.
    // Code Implementation $('ul').on('click', 'li', function() { $(this).addClass('active').siblings().removeClass('active'); }); 

More Tags

angularjs-ng-change mutablelivedata filezilla mongodb-.net-driver ctypes animated-gif azure-devops-wiki gradle-task webforms jasmine-node

More Programming Questions

More Cat Calculators

More Fitness Calculators

More Geometry Calculators

More Math Calculators