Reputation: 105
$(function () {
var string = 'http://i.imgur.com/eHq5oXc.jpg'
$('div').css('background-image', string);
});
What's wrong in my code above? It looks fine for me.
Upvotes: 0
Views: 331
Reputation: 3610
Try this one:
var string = 'http://i.imgur.com/eHq5oXc.jpg';
$('div').css('background-image', 'url(' + string + ')');
Upvotes: 2
Reputation: 450
You can get it only with CSS:
div {
width:50px;
height:50px;
background-color:pink;
background: no-repeat url('http://i.imgur.com/eHq5oXc.jpg');
background-size:cover;
}
DEMO: http://jsfiddle.net/twsfag9k/6/
Upvotes: 0
Reputation: 4656
$(function () {
var string = 'url(http://i.imgur.com/eHq5oXc.jpg)';
$('div').css('background-image', string);
});
Check below fiddle link :
http://jsfiddle.net/twsfag9k/2/
Upvotes: 0