JQuery - css ("background-image", variable) doesn't work :(

I am trying to save the background image of an element, delete it, and then maybe add it 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 that the background image is supposed to be added to does not work ... But if I changed current_bg_imageto "url(something.jpg)", it will work. Does css () seem to not work with variables?

+3
source share
2 answers

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

+4

, :

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

+10

All Articles