Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a div's background image with jQuery. Is there an inbuilt method to strip out the url() portion?

I am using this code to get the background image of a div.

var bgImage = $('#content').css('backgroundImage');

This is returning url%28http://example.com/images/layout/content-trans.png%29

I know you can do element.height() to get an element's height without px appended (parseInt() also works), so I was wondering if there was a similiar method for jQuery to get the actual background image minus the url() meta data.

I suppose I could use a regular expression, something like /url\((.*)\)/, but I'd rather know first if there is an built-in way.

like image 850
alex Avatar asked May 08 '09 00:05

alex


1 Answers

url() is part of the value of background-image, so I guess you need to use regex replace.

var bgImage = $('#content').css('background-image').replace(/^url|[\(\)]/g, ''); 

Ref: http://groups.google.com/group/jquery-en/browse_thread/thread/d866997cb206b35f

like image 176
Brian Kim Avatar answered Oct 13 '22 00:10

Brian Kim