0

http://jsfiddle.net/twsfag9k/

$(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.

3 Answers 3

2

Try this one:

var string = 'http://i.imgur.com/eHq5oXc.jpg';

$('div').css('background-image', 'url(' + string + ')');
Sign up to request clarification or add additional context in comments.

1 Comment

what if I can;t modify the string?
0
$(function () {
    var string = 'url(http://i.imgur.com/eHq5oXc.jpg)';
    $('div').css('background-image', string);
});

Check below fiddle link :

http://jsfiddle.net/twsfag9k/2/

Comments

0

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/

Comments

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.