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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:54:37+00:00 2026-06-05T04:54:37+00:00

So I’m setting up a background changer for accessibility purposes for a client. I

  • 0

So I’m setting up a background changer for accessibility purposes for a client. I have a set of div’s with the required bg color, and the jQuery to change the body background on click.

What I want is for the clicked element to change to white (ie, a reset option) on click.

But with the code I have, if you click on another, it also turns white, without resetting the other, so you end up with a whole bunch of white divs.

here is my code:

<div class="bg_changer">
                <ul><li class="bg_1 bg_setter"><a >bg_one</a></li>
                <li class="bg_2 bg_setter" ><a >bg_two</a></li>
                <li class="bg_3 bg_setter"><a >bg_three</a></li>
                <li class="bg_4 bg_setter"><a >bg_four</a></li>
                <li class="bg_5 bg_setter"><a >bg_five</a></li>
                <li class="bg_6 bg_setter"><a >bg_six</a></li>

                </ul>

</div><!-- end of bg_changer -->

css

.bg_1{
    background-color: rgba(204,204,204,1);
}
.bg_2{
    background-color: rgba(254,254,196,1);
}
.bg_3{
    background-color: rgba(253,190,130,1);
}
.bg_4{
    background-color: rgba(253,253,128,1);
}
.bg_5{
    background-color: rgba(158,208,253,1);
}
.bg_6{
    background-color: rgba(218,218,254,1);
}

and jQ

$(document).ready(function(){
    $(".bg_setter").click(function() {
      var bg_new =  $(this).css('background-color');
        $("body").css('background-color', bg_new);
        $(this).css('background-color', 'white');
    });

});

Does anyone have any ideas about how to implement this change, so that if I switch bg_one, but then switch bg_two, I can reset bg_one to it’s original background color?

I can’t think of a way to do this without storing a tonne of variables, surely there must be something easier…

  • 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-05T04:54:39+00:00Added an answer on June 5, 2026 at 4:54 am

    One way would be to set a global variable which holds the Selected element, and the selected element’s color. When you click another, it sets the previously selected element and color back, using those variables.

    $(document).ready(function() {
        //global vars - nothing yet, no color has been selected
        var prev_element, prev_color;
    
        $(".bg_setter").click(function() {
            //if there has been a previously selected element - set it's color back
            if (prev_element) {
                prev_element.css('background-color', prev_color);
            }
    
            //Store this element and the current color
            prev_element = $(this);
            prev_color = $(this).css('background-color');
    
            //Update the body background color with the color selected
            $("body").css('background-color', prev_color);
    
            //set the selected element background color to white
            prev_element.css('background-color', 'white');
        });
    
    });​
    

    DEMO: http://jsfiddle.net/AY2B3/2/

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.