I was wondering how to use jQuery to find the url of a div
s background-image
and append it to another div
. How can I do this?
Asked
Active
Viewed 43 times
0

user3105120
- 307
- 1
- 3
- 9
-
do you have your work so far, or some sample code we can look at? – Sammy Aug 14 '14 at 03:14
-
check this one.. http://stackoverflow.com/questions/8809876/can-i-get-divs-background-image-url – reuelab Aug 14 '14 at 03:16
1 Answers
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