1

I currently have a DIV with a background image set as follows:

background: url(../images/site/common/body-bannar-bkground.png) repeat 0 0;

How can I remove this image and set a background-color only:

background-color: #C1A3A5

I know I can use JQUERY To set the new color like this

$('div#id').css('backgroundColor', '#C1A3A5');

but when I do this the background image is still in place. How can I knockout the background image? I also need to do this in reverse so how can I knock out the background-color?

Finally - would I be better to addClass() removeClass with the attributes set there or is basically the same factor?

thx

3 Answers 3

4

Just use the background shorthand rule to overwrite it:

$('div#id').css('background', '#C1A3A5');
Sign up to request clarification or add additional context in comments.

Comments

3

Messing with CSS directly in JS is never a good idea.

Better to use different classes for different purposes, with different CSS rules, and apply/remove them accordingly. You want to keep JS, CSS and HTML separated from each other as much as possible.

Your CSS:

#id {
  background-position: 0 0;
  background-repeat:repeat;
  background-color: #C1A3A5;
}

.backgroundImage {
  background-image: url(../images/site/common/body-bannar-bkground.png);
  /* background-color: add different color if needed */
}

Your JS:

 $('#id').removeClass('backgroundImage');

You only need to add or remove the backgroundImage class to your div if you want it to be displayed or not. Other background settings won't change.

Comments

0

Just Try this :

$("div#id").css("background","");
$('div#id').css('backgroundColor', '#C1A3A5');

First Statement will remove background completely and second will attach only color to background

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.