0

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?

Huangism
  • 16,278
  • 7
  • 48
  • 74
  • possible duplicate of [Setting background-image using jQuery CSS property](http://stackoverflow.com/questions/512054/setting-background-image-using-jquery-css-property) – Huangism Apr 20 '15 at 18:21

2 Answers2

3

This can be done by using $('.header-class').css('background-image', 'url(' + someimage + ')');, where you replace .header-class with... your header's classname.

You can read up on it here

xyhhx
  • 6,384
  • 6
  • 41
  • 64
0

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 +')');
Crowlex
  • 101
  • 7