How can I dynamically change the background-color html?

I want to change the background (gradient) of either the html or body in response to an event. Is it possible to change the background-color attribute using jQuery? Or (I have the background gradients described in CSS classes*) do I need to removeClass then addClass? If the latter, I have a slight dilemma: I am going to use three different colors. Do I need to remove both of the other colors via removeClass (even though, obviously, only one will be active), or is there a way to programmatically determine which one is currently in use and just remove that? I reckon I could use a global var atop the ready function to keep track of that, but is there a better/more elegant way?

Upvotes: 1

Views: 1884

Answers (1)

iConnor
iConnor

Reputation: 20189

This is how i would do it

$('someElement').click(function(){ // 1
   var newClass = $(this).attr('toggleColor'); // 2
   $(document.body).removeClass('color1 color2 color3').addClass(newClass); // 3
});

----- Details ------

steps

  1. Add a click event to the element that will toggle a different color
  2. Get the new class name i am assuming that you will have this in a attribute on the element
  3. Remove every color class from the element then add the new one

Upvotes: 1

Related Questions