David19801
David19801

Reputation: 11448

jquery background from image to color and back again

I have a html page, with jquery loaded. It has no css at the moment.

It has a background image set as:

<body background="image1.gif">

I'm trying to change this to a color (say black) and back again to the image using onclick.

I've managed to set up the divs and all ok. For changing color to color I used:

jQuery('body').css('background-color','black');

on another page, but that doesn't work here because i want to go from image to color and back again.

So, how can I change the background image to a flat color and back again with jquery?

Upvotes: 0

Views: 2224

Answers (1)

Guidhouse
Guidhouse

Reputation: 1426

Use css and change the class instead:

The HTML:

<body class="image">

The CSS:

body.image{
  background-image: url('image.gif');
}
body.colour{
  background-color: black;
}

And for scripting:

$('body').addClass('colour').removeClass('image');

And back again:

$('body').addClass('image').removeClass('colour');

Upvotes: 3

Related Questions