3

I'm trying to save the background-image of a element, remove it and then maybe add it back later.

var current_bg_image = $("#div").css("background-image");
if(something){
  $("#div").css("background-image", "none");
}else{
  $("#div").css("background-image", current_bg_image); // not working...
}

The part where the background image is supposed to be added back doesn't work... But if I change current_bg_image with "url(something.jpg)" it works. It seems that css() doesn't work with variables?

2 Answers 2

11

Had the same problem, solved by this:

var id = $(this).attr("src"); //alert(id); $('#div').css({'background-image': 'url(' + id + ')', });

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

Comments

4

Your code works fine for me using jQuery 1.5.1. I believe what you will get once saving the background-image value to current_bg_image is the full path to the image, so try reading that value using Firebug or alert(current_bg_image) and ensure it is what you think it should be.

1 Comment

tx, actually the problem was that var current_bg_image = $("#div").css("background-image"); was running everytime the function was called, I put it outside and now its ok :)

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.