MegaTron MegaTron - 4 months ago 8
jQuery Question

Background-image style attribute in JavaScript

I'm trying to apply a background image using a style tag on a div that's reference in JavaScript. I'm sure it's something to do with the quotations around the image variables. Any help would be greatly appreciated.

function appendData () {
var image = "http://domain.com/image.jpg";
var html = '<div class="itemImage" style="background-image:url(' + image + ');"></div>';
$('.container').append(html);
}

Answer
function appendData () {
 var image = "http://domain.com/image.jpg";
 var html = '<div class="itemImage" style="background-image:url(\'' + image +    '\');"></div>';
 $('.container').append(html);
}
Comments