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 561263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:25:53+00:00 2026-05-13T12:25:53+00:00

This is my code to switch the class of my body tag when a

  • 0

This is my code to switch the class of my body tag when a user clicks a link.

function switchBodyColor() {
    if (document.body.className == 'blue')
        document.body.className = 'red';
    else if (document.body.className == 'red')
        document.body.className = 'green';
    else if (document.body.className == 'green')
        document.body.className = 'blue';
}

I want to set the resulting color as a variable called bodyColor. So if the body class is “blue” and the user clicks and switches it to “red” I want to store red as a variable (bodyColor) for other uses later on. Or better yet, set document.body.className as a variable itself and then switch out document.body.className in the switchBodyColor() function with that variable.

I thought something along the lines of:

    if (document.body.className == 'blue')
        document.body.className = 'red',
        var bodyColor = red;

or

var bodyColor = document.body.className

to set the body class as a variable.

Of course neither of those two options work. ^_^; How can I accomplish either (or both) of the above?

  • 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-05-13T12:25:53+00:00Added an answer on May 13, 2026 at 12:25 pm

    You need your variable to be global:

    var bodyColor = 'red';  // Global var, initialized to your first color class
    
    function switchBodyColor() {
        if (document.body.className == 'blue')
            document.body.className = 'red';
        else if (document.body.className == 'red')
            document.body.className = 'green';
        else if (document.body.className == 'green')
            document.body.className = 'blue';
    
    
        bodyColor = document.body.className;
        alert(bodyColor);
    }
    

    In your other example, you also need to put quotes around your color string:

     bodyColor = "red";
    

    Another way to do this might be to number your color classes, which will let you use simple arithmetic to change your classes and allows you to easily change the number of classes you are cycling through.

    var colorNum = 0;
    var totalColors = 3;
    
    function switchBodyColor() {
        colorNum = (colorNum+1)%totalColors;
        document.body.className = 'color'+colorNum;
    }
    

    You css would be:

    .color0 { background-color: blue; }
    .color1 { background-color: red; }
    .color2 { background-color: green; }
    

    Or whatever your color class definitions are.

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

Sidebar

Related Questions

This code attempts to dynamically switch the class of the StateContainer div from StateOne
Say I have a class Code defined like this, with a user specified type
I have some code that looks like this: someFunc(value) { switch(value){ case 1: case
This code always works, even in different browsers: function fooCheck() { alert(internalFoo()); // We
Consider the following code : HTML: <div class='a'></div> <div class='b'></div> CSS: body { position:
I have this code snippet that I want to simplify: switch (typeString) { case
I have this code which is looking for an onchange event: //Create destination switch
This code in JS gives me a popup saying i think null is a
This code is from Prototype.js . I've looked at probably 20 different tutorials, and
this code always returns 0 in PHP 5.2.5 for microseconds: <?php $dt = new

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.