Reputation: 69
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.
Upvotes: 2
Views: 4962
Reputation: 92
$(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>
Upvotes: 0
Reputation: 1520
I sit this to get the value from element with inline style properties.
var imgUrl = $("#image").prop("style", "background-image");
Upvotes: 0
Reputation: 19070
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)">
Upvotes: 1