Chaitanya K
Chaitanya K

Reputation: 1852

include background image using javascript

i want to apply background image with no repeat using javascript..

#img_name{

    background-image:
    background-repeat:
}

how to convert this accordingly

 $("#img_name").html("<img src=assets/\images\/"+data['img_name']+">");

Upvotes: 1

Views: 59

Answers (1)

Alex Tartan
Alex Tartan

Reputation: 6836

Try this:

$('#img_name').css('background-image', 'url("assets/images/"'+data['img_name']+'")');
$('#img_name').css('background-repeat', 'no-repeat');

Or, setting both in one go:

$('#img_name').css({
    'background-image' : 'url("/assets/images/'+data['img_name']+'")',
    'background-repeat': 'no-repeat'
});

Upvotes: 2

Related Questions