3

I have a element with following style:

.element {
    background: transparent url(backgroundimage1.png) no-repeat,
        transparent url(backgroundimage2.png) no-repeat;
}

Is it possible to change only the second background url using JavaScript or jQuery without a lot of code?

4
  • Try this: $('.element').css('background', 'transparent url(backgroundimage1.png) no-repeat, transparent url(newimage.png) no-repeat'); Commented Dec 3, 2015 at 13:23
  • @sfandler I hope this answer works for you, if it does please mark it as correct :D. Commented Dec 3, 2015 at 15:52
  • @Zoheiry Oh, yea thanks.. I upvoted but somehow forgot to accept the answer haha. Commented Dec 3, 2015 at 17:01
  • @sfandler Thanks, feel free to ask me anything at anytime ;) Commented Dec 3, 2015 at 17:40

1 Answer 1

4

Yes it is possible with a little string manipulation using jQuery.

var first_background = $(".element").css('background').split(',')[0];
var second_background = $(".element").css('background').split(',')[1];
var new_background = //add new background here.
$(".element").css('background', first_background + ',' + new_background);

And you're done.

Sign up to request clarification or add additional context in comments.

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.