Marksyw Marksyw - 1 month ago 10
Javascript Question

Get CSS background-image url from ID

how to get the url from this ID

<div id="image" style="background-image: url(/test.com/test.jpg)">


Has been searching google and stack overflow for this but only getting "get id from url" or current window location result only.

Answer

You can use element style backgroundImage property:

var img = document.getElementById('image'),
    url = img.style.backgroundImage;

console.log(url);
<div id="image" style="background-image: url(/test.com/test.jpg)">

Comments