1

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']+">");

1 Answer 1

2

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'
});
Sign up to request clarification or add additional context in comments.

3 Comments

it must be from <?php echo data['img_name']; ?>. Replace it with a value. You can't run PHP in a .js file (or test the script in a .php file)
try 'url("assets/images/"'+data['img_name']+'")'
You can't set CSS properties trough html(). That's what css() is for.

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.