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 in JQuery by Edureka
• 13,640 points
141 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 by gaurav
• 16,820 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,740 points
2,189 views
0 votes
1 answer

How to add class using jQuery?

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

answered Jun 1 in JQuery by Edureka
• 13,640 points
35 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 in JQuery by Edureka
• 13,640 points
172 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 in JQuery by Edureka
• 13,640 points
22 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
186 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
596 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,500 points
305 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,740 points
278 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 in JQuery by gaurav
• 16,820 points
18 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 in JQuery by gaurav
• 16,820 points
33 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP