Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8637635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:31:33+00:00 2026-06-12T10:31:33+00:00

I want to add/remove three css classes plus a default classes. Example: When I

  • 0

I want to add/remove three css classes plus a default classes. Example: When I click on the first anchor it adds a new class called style1 and removes the default style, when I click on the second anchor it removes style1 and adds style2, and the same for anchor3.

The problem is if I start by clicking on anchor3, nothing happens, I cannot reverse the action.

How can I make this work so that the add/remove classes works regardless of which link I click first? I need to use the three styles but also need a default style.

Here is the code on JFiddle: http://jsfiddle.net/VadUE/2/

It works in the browser but not on JFiddle.

Code:

$(document).ready(function() {
    $('.anchor1').click(function() {
        $('p').addClass('style1').removeClass('default');
        $('.anchor2').click(function() {
            $('p').addClass('style2').removeClass('style1');
            $('.anchor3').click(function() {
                $('p').addClass('style3').removeClass('style2');
            });
        });
    });
});


<style type="text/css">
.style1{
    color: #FF8000;
}
.style2{
    color: #F36;
}

.style3{
    color: #00F;
}

.default{
    color: #0C0;
}
</style>


<a class="anchor1" href="#/">Link1</a>
<a class="anchor2" href="#/">Link2</a>
<a class="anchor3" href="#/">Link3</a>
<p class="default">This is a sequence into a sound</p>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:31:34+00:00Added an answer on June 12, 2026 at 10:31 am
    1. In your jsfiddle, you have chosen the mootools library.
    2. In the ready method you are nesting all click actions.
    3. If you remove a class, the classname to remove has to be retrieved.

    Fixing this (and removing the need for ready call by placing the script at the bottom of the page), you end up with:

    $('.anchor1').on('click',function(){
        $('p').removeClass(function() {
              return $(this).attr('class');
            }).addClass('style1');
    });
    $('.anchor2').click(function(){
        $('p').removeClass(function() {
              return $(this).attr('class');
            }).addClass('style2');
    });
    $('.anchor3').click(function(){
        $('p').removeClass(function() {
              return $(this).attr('class');
            }).addClass('style3');
    });
    

    See jsfiddle

    Using a single className for the links and adding an numeric id you would only need one handler:

    $('.anchor').on('click',function(){
        $('p').removeClass(function() {
              return $(this).attr('class');
            }).addClass('style'+this.id);
    });​
    

    See jsfiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I want to be able to add/remove class methods at runtime. Before you
What would be the best way to add/remove a css class to a div
I want to add and remove div element using jquery and adding of div
I have Panel with a BottomToolbar . I want to add and remove dynamically
I want to add row with remove button through javascript in html div formate.
What I want to do is add and remove list items. I have got
I want add new Feed item on entity persist and update. I write this
I want to dynamically add some columns (and remove others) from an html table.
in my project i want to implement a modul which add or remove contrast
I'm trying to add and remove classes dynamically using jQuery in my webpage. Here;s

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.