Marksyw
Marksyw

Reputation: 69

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.

Upvotes: 2

Views: 4962

Answers (5)

Jay Prakash
Jay Prakash

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

andre mcgruder
andre mcgruder

Reputation: 1520

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

var imgUrl = $("#image").prop("style", "background-image");

Upvotes: 0

Yosvel Quintero
Yosvel Quintero

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

Zaki Mustafa
Zaki Mustafa

Reputation: 147

You can do it like this

   $("#image").css("background-image");

Upvotes: 1

martavoi
martavoi

Reputation: 7092

Using jQuery:

$('#image').css('background-image')

Upvotes: 1

Related Questions