jQuery onclick toggle class name

0 votes

Using jQuery, how do I toggle classA to classB on click going from:

<a class="switch" href="#">Switch</a>
<div class="classA"></div>


$('.switch').on('click', function(e){
    $('.classA').removeClass('classA').addClass('classB');
    e.preventDefault();
};

How do I toggle the class, instead of just replacing it like I'm doing here?

May 27, 2022 in JQuery by Edureka
• 13,690 points
3,234 views

1 answer to this question.

0 votes
jQuery toggleClass () method
 The tokenClass () method toggles between adding and removing one or more class names from the selected element. This method checks each element for the specified class name. If the class name is missing, it will be added, and if it is already set, it will be deleted. This creates a toggle effect.
answered May 30, 2022 by gaurav
• 23,260 points

Related Questions In JQuery

0 votes
1 answer

How to find a parent with a known class in jQuery?

Hello @kartik, Assuming that this is .d, you can write $(this).closest('.a'); The closest method returns the ...READ MORE

answered Oct 5, 2020 in JQuery by Niroj
• 82,840 points
7,289 views
0 votes
1 answer

How to add class using jQuery?

Add a class name to the first ...READ MORE

answered Jun 1, 2022 in JQuery by Edureka
• 13,690 points
3,226 views
0 votes
1 answer

addClass and removeClass in jQuery - not removing class

What happens is that your close button ...READ MORE

answered Jun 2, 2022 in JQuery by Edureka
• 13,690 points
10,884 views
0 votes
1 answer

Get class list for element with jQuery

Use the jQuery attr() Method You can simply use the attr() method ...READ MORE

answered Jun 1, 2022 in JQuery by Edureka
• 13,690 points
6,238 views
0 votes
1 answer

How to use jquery with asp.net ajax?

If you weren't aware, Microsoft is planning ...READ MORE

answered Oct 15, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
763 views
0 votes
1 answer

Is 'sparkline' a method?

I suggest you to check 2 things That jquery.sparkline.js is actually ...READ MORE

answered Nov 9, 2018 in Apache Spark by Frankie
• 9,830 points
1,301 views
0 votes
1 answer

What is jQuery?

Hey, jQuery is a fast and concise JavaScript ...READ MORE

answered Feb 14, 2020 in JQuery by kartik
• 37,520 points
1,347 views
0 votes
1 answer

Error: Global Variable is not accessable to local function

Hey kartik, A variable declared outside a function has a ...READ MORE

answered Feb 19, 2020 in PHP by Niroj
• 82,840 points
1,104 views
0 votes
1 answer

JQuery class selectors like $(.someClass) are case sensitive?

jQuery attribute value selectors are generally case-sensitive. ...READ MORE

answered May 30, 2022 in JQuery by gaurav
• 23,260 points
691 views
0 votes
1 answer

Jquery resize image with a heart beat effect

This code should work  (function pulse(back) { $('#seventyfive img').animate( ...READ MORE

answered May 30, 2022 in JQuery by gaurav
• 23,260 points
703 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP