Search code examples
jquerycssvariablesbackground-imagesrc

How to use a variable for img src?


I am using this script to get the image src of an article's primary image

var someimage = $("body").find(".default-content").find('img').attr("src");

now I want to pass that var into my

background: url();

This will be the background image of the header but it will be different for each article page. Is there a way to use the var as the src or could this be done with jQuery?


Solution

  • You can use something like this

    var someimage = $("body").find(".default-content").find('img').attr("src");.
    
    // set background url
    $('#someblock').attr('style','background-image: url('+someimage +')');