javascript - Add and remove class on click - Stack Overflow

July 07, 2022 | Admin |

javascript - Add and remove class on click - Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

I have a tab menu, and i want onlick be be added a class="selected" - and on click on one of the other tabs, the class should be removed from the current link, and then be added to the link i've clicked on...

I've tried this but didnt work

And the HTML:

this is the target of the click event toggleClass method adds a class if it is not present else removes it.

Therefore when you say $(this).toggleClass('selected');, The class is added or removed only on the element that was clicked which is clearly not what you want.

Using pure javascript:

codepen http://codepen.io/8eni/pen/MaGVrq

Step: 1 - write the following code in .html file

Step: 2 - Write the following in .js file

Step: 3 - Write the following in .css file

This works for me. try to use this code.

Thanks for contributing an answer to Stack Overflow!

But avoid …

To learn more, see our tips on writing great answers.

Required, but never shown

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2022 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2022.7.7.42548

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Related Posts

Register for Classes - University of Colorado Denver

You can register for classes beginning on your specified enrollment appointment date/time, all the way through open enrollment. Once it's your time to register, head back into UCDAccess to finish enrolling. Select “Enrollment Shopping Cart” from the “Register for Classes” menu. Check the box for each of the classes you wish to enroll in.

First time registering? Need a refresher? Check out the step-by-step checklist.

Start planning early!

It's important to plan your class schedule early each semester and register as soon as you can. Enrollment appointments (date/time) are issued prior to open enrollment, based on the number of credits completed so be sure to find your enrollment appointment in the UCDAccess student portal prior to each semester's class registration period.

It's always a good idea...

Read more >>

Pilot Onboarding | Civil Air Patrol

Once you have become a Civil Air Patrol (CAP) member you will begin your level 1 training. This is similar to the training you recieve when you start a new corporate job. ... Becoming a …

Once you have become a Civil Air Patrol  (CAP) member you will begin your level 1 training. This is similar to the training you recieve when you start a new corporate job.  We know you are eger to get into the cockpit and we can't wait to start your pilot onboarding.

Becoming a Civil Air Patrol pilot can be rewarding and fulfilling, as a CAP pilot you are given the opportunity to fly aircraft that are part of the world’s largest fleet of Cessna aircraft.  In...

Read more >>

Transportation & D.O.T. Training Videos & Courses

Transportation & D.O.T. safety training videos for hazardous materials, cargo securement, inspections, road rage, drugs and alcohol and more. ... The “Injury Prevention for CDL Drivers” training video discusses the most common types of injuries that are experienced by CDL drivers, and how they can be avoided. ... Subscribe to our free: HR ...

Our course on “DOT Hours of Service” discusses the Department of Transportation’s “Hours of Service” regulations, what the regulations themselves require, and what drivers need to do to comply with them.

The “Injury Prevention for CDL Drivers” training video discusses the most common types of injuries that are experienced by CDL drivers, and how they can be avoided.

This training course “Driving Defensively for CDL/Large...

Read more >>