How can I remove a background-image attribute?

Viewed 74776

I have this code :

if (pansel.val() == "1") 
     $(#myDiv).css('background-image', 'url(/private_images/guida_check_category.jpg)'); 
else 
     $(#myDiv).css({ 'background-color': '#ffffff' });

and I see that, when I apply the background-image, the follow code :

$(#myDiv).css({ 'background-color': '#ffffff' });

doesnt remove the image and put the white background color. The image still present!

How can I totally remove that background-image attribute?

7 Answers

Get, Set and Delete Div Background Image In jQuery

Get Background image

You can use the below code for getting the background image in jQuery.

$(".btn-get").click(function() {
var bg = $('div').css('background-image'); // get background image using css property
bg = bg.replace('url(','').replace(')','');
alert(bg); });   

Set Background Image

You can use the below code for setting the background image using the jQuery CSS property.

 $(".btn-set").click(function() {
 var img = 'http://www.tutsmake.com/wp-content/uploads/2019/08/How-to-Encode-Decode-a-URL-Using-JavaScript.jpeg';
 var bg = $('div').css('background-image',"url(" + img + ")");
});

Remove Background Image

You can use the below code for removing the background image using the jQuery CSS property.

$(".btn-remove").click(function() {
    var bg = $('div').css('background-image','none');
}); 

$(element).css('background-image', '')

I don't know what is happens in backstage but img is removed from UI.

Related