0

I was wondering how to use jQuery to find the url of a divs background-image and append it to another div. How can I do this?

user3105120
  • 267
  • 1
  • 2
  • 8

1 Answers1

1
var bg = $('#source').css('backgroundImage')
$('#target').css('backgroundImage', bg)

You can also do it easily without jQuery:

var bg = document.getElementById('source').style.backgroundImage
document.getElementById('target').style.backgroundImage = bg

If you only want the url content of the property:

var re = /url\((.+)\)/  // assuming the format: url(/path/to/image.png)
var bg = $('#source').css('backgroundImage')
var url = re.exec(bg)[1]
$('#target').css('backgroundImage', url)
Hanuman
  • 6,771
  • 3
  • 41
  • 39