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
  • 307
  • 1
  • 3
  • 9

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)
Jaime Gómez
  • 6,961
  • 3
  • 40
  • 41