2

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.

Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
Marksyw
  • 69
  • 2
  • 6
  • 1
    Possible duplicate of [Can I get div's background-image url?](http://stackoverflow.com/questions/8809876/can-i-get-divs-background-image-url) – Mohammad Oct 22 '16 at 17:11
  • Does this answer your question? [Get URL from background-image Property](https://stackoverflow.com/questions/6397529/get-url-from-background-image-property) – Jason C Sep 08 '20 at 16:23

5 Answers5

1

Using jQuery:

$('#image').css('background-image')
Dzmitry Martavoi
  • 6,867
  • 6
  • 38
  • 59
1

You can do it like this

   $("#image").css("background-image");
Zaki Mustafa
  • 147
  • 1
  • 11
1

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)">
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
0

I sit this to get the value from element with inline style properties.

var imgUrl = $("#image").prop("style", "background-image");
andre mcgruder
  • 1,120
  • 1
  • 9
  • 12
0

$(document).ready(function(){
 var x=$("#image").css("background-image");
 var imageurl = x.substring(5,x.length-2);
 alert(imageurl);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<div id="image" style="background-image: url(//test.com/test.jpg); height: 230px" class="testclass">Image</div>
Jay Prakash
  • 92
  • 1
  • 8