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

  • SEARCH
  • Home
  • 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 8980457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:05:35+00:00 2026-06-15T20:05:35+00:00

So I have some nav anchors. I want to change some basic color schemes

  • 0

So I have some nav anchors. I want to change some basic color schemes on the page when these are clicked. The only way I’ve been able to do this is with the code below.

It works fine, but I can’t help but think there is a more elegant way. Does anybody know if it’s possible to strip the classes in one foul swoop?

EDIT: This could have been more clear. I didn’t want the original classes gone. Just the classes added with jQuery I guess. Sorry for the confusion.

Thanks.

EDIT: SEE THIS FIDDLE

Tried to implement all solutions.

getting an “{“error”: “Please use POST request”}” though…

EDIT: this was because of href=”?” vs. href=”#” as pointed out.

    $(".home").click(function() {
        $(".primary-color").addClass("one");
            $(".primary-color").removeClass("two"); 
            $(".primary-color").removeClass("three");   
            $(".primary-color").removeClass("four");    
            $(".primary-color").removeClass("five");           
    });

    $(".services").click(function() {
        $(".primary-color").addClass("two");
            $(".primary-color").removeClass("one");
            $(".primary-color").removeClass("three");   
            $(".primary-color").removeClass("four");    
            $(".primary-color").removeClass("five");       
    });

    $(".pricing").click(function() {
        $(".primary-color").addClass("three");
            $(".primary-color").removeClass("one");
            $(".primary-color").removeClass("two");     
            $(".primary-color").removeClass("four");    
            $(".primary-color").removeClass("five");       
    });

    $(".special-thing").click(function() {
        $(".primary-color").addClass("four");
            $(".primary-color").removeClass("one");
            $(".primary-color").removeClass("two"); 
            $(".primary-color").removeClass("three");   
            $(".primary-color").removeClass("five");   
    });

    $(".contact").click(function() {
        $(".primary-color").addClass("five");
            $(".primary-color").removeClass("one");
            $(".primary-color").removeClass("two"); 
            $(".primary-color").removeClass("three");   
            $(".primary-color").removeClass("four");     
    });
  • 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-15T20:05:37+00:00Added an answer on June 15, 2026 at 8:05 pm

    You could do it this way

    http://jsfiddle.net/lollero/ZUJUB/

    $(document).ready(function() {
        $(".home, .services, .pricing, .special-thing, .contact").on("click", function() {
    
            $(".primary-color")
                // Remove all classes
                .removeClass()
                // Put back .primary-color class + the clicked elements class with the added prefix "pm_".
                .addClass('primary-color pm_' + $(this).attr('class') );       
        });
    });​
    

    CSS:

    .primary-color {
        width: 100px;
        height: 100px;
        border: 1px solid #e1e1e1;
    
    }
    
    
    .pm_services { background: red; }
    .pm_home { background: green; }
    ​
    

    HTML:

    <div class="services">services</div>
    <div class="home">home</div>
    
    <div class="primary-color">Primary-color</div>
    ​
    


    or something like this:

    http://jsfiddle.net/lollero/ZUJUB/1/

    This fitted into the OP’s structure: http://jsfiddle.net/lollero/EXq83/5/

    jQuery:

    $(document).ready(function() {
        $("#buttons > div").on("click", function() {
    
            $(".primary-color")
                // Remove all classes
                .removeClass()
                // Put back .primary-color class + the clicked elements index with the added prefix "pm_".
                .addClass('primary-color pm_' + ( $(this).index() + 1 ) );       
        });
    });​
    

    CSS:

    .primary-color {
        width: 100px;
        height: 100px;
        border: 1px solid #e1e1e1;
    
    }
    
    
    .pm_1 { background: red; }
    .pm_2 { background: green; }
    ​
    

    HTML:

    <div id="buttons">
        <div class="services">services</div>
        <div class="home">home</div>
    </div>
    
    <div class="primary-color">Primary-color</div>
    ​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that will change the background color of a specific label
I have some html with the basic structure <nav> <div> <a href=> <div class=navlink
I have some arbitrary pixel data that I want to save as a PNG.
I'm have some that uses jQuery.clone() to get the html of a page and
I have setup some tabbed navigation on my ASP.NET Masterpage. It is pretty basic:
Sorry for my crazy question. I have some nav link. By mouseover the links
I have some views in my app that I don't want to suport orientation.
I'm not sure what I am doing wrong here. I have some basic markup.
This has been bothering me for awhile. I have some pages on my website
I have some links to my facebook and twitter, these links are images. I

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.