FlyingCat
FlyingCat

Reputation: 14260

how to replace background image in my case?

I am trying to change a background image when user click a button.

after user click a button the following code execute.

 $('body').css('background',"url('test2.png') no-repeat center center")

however, My css has more attributes like the following:

body {
    background: url('test.png') no-repeat center center;
    -webkit-background-size: cover;
    background-color: #00ABBA;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

By execute my js code, it will kill all other css attribute and only replace the background image. I know I can append all the css attribute to my js but I feel like there is a better way to do this. Can anyone help me about it? Thanks!

Upvotes: 0

Views: 51

Answers (1)

Nicholas Hazel
Nicholas Hazel

Reputation: 3750

$('body').css('background-image','url(test2.png)');

Just target background-image rather than the universal background.

All of your other styles will remain in place.

Upvotes: 3

Related Questions