2

Possible Duplicate:
Get URL from background-image Property

I'm using the Cycle Plugin and i want to create a pager/thumbnail.

HTML

<div id="slide">
  <div class="panel" id="product">

  </div>
</div>

My Image is on the background of the div. Not inside the div itself

JS Call

pager:'#thumbs', pagerAnchorBuilder: function(idx, slide) { return '<li><a href="#"><img src="' + slide.src + '" width="50" height="50" /></a></li>'; },

return '<li><a href="#"><img src="' + slide.src + '" width="50" height="50" /></a></li>';

I'm trying to grab the background image on product. So instead of returning + slide.src + what would i replace this with?

The above grabs the source of the image. How can i grab the background source on the div rather than the image source?

My image is on the div not inside a <img> tag.

**Thanks. **

Community
  • 1
  • 1
breezy
  • 1,910
  • 1
  • 18
  • 35

4 Answers4

3

Not 100% sure on which div you're wanting the background image of, and where you're wanting to use it, but...

$('#product').css('background-image') will give you the URL to the background image of the product div.

So, following on from this, in your code I think you'll need it changing to:

pager:'#thumbs', pagerAnchorBuilder: function(idx, slide) { return '<li><a href="#"><img src="' + $("#product").css("background-image") + '" width="50" height="50" /></a></li>'; },

Final answer, thanks to rsp:

pager:'#thumbs', pagerAnchorBuilder: function(idx, slide) { return '<li><a href="#"><img src="' + $("#product").css("background-image").replace('url(', '').replace(')', '') + '" width="50" height="50" /></a></li>'; }
Chris Dixon
  • 9,147
  • 5
  • 36
  • 68
  • 1
    It won't work because the `background-image` CSS property is in the form `url(http://www.example.com/image.jpg)` so you need to remove the "url(" and ")" - see my answer. – rsp Sep 21 '12 at 15:13
  • 1
    Good shout, didn't think of that! – Chris Dixon Sep 21 '12 at 15:15
  • this is almost working. However it is also grabbing the parenthesis after the URL `)` – breezy Sep 21 '12 at 15:19
  • 1
    pager:'#thumbs', pagerAnchorBuilder: function(idx, slide) { return '
  • '; } - that'll work, thanks to rsp. – Chris Dixon Sep 21 '12 at 15:23
  • I appreciate the help Guys. @rsp I'm going to accept this answer and up vote yours. – breezy Sep 21 '12 at 15:25
  • what if there are multiple selectors that i want to grab the background image from? for example `$("#product, #product2")` doesnt work. help please @thedixon @rsp – breezy Oct 17 '12 at 20:15
  • $("#product, #product2").each(function(i,val) { $(this).css('bacckground-image') }); – Chris Dixon Oct 18 '12 at 08:02