Wednesday, July 08, 2009

jQuery Quick Tip: Extract CSS Background Image

jQuery allows you to get the background image of any element on a web page:


However, this returns it in an undesirable format: url( or url(""). With a bit of string replacement, you can get extract the URL:

function extractUrl(input)
 // remove quotes and wrapping url()
 return input.replace(/"/g,"").replace(/url\(|\)$/ig, "");

So now you can just do this:


Which will return the URL on its own


Renowned Media said...

Regular expressions to the rescue!

H. K. Nuhu said...

This seems to be a very useful blog for a web developer like me. I have been using jQuery and your blog site has some useful info. Thanks.