Can I get div's background-image url?

jQuerybeast picture jQuerybeast · Jan 10, 2012 · Viewed 144.8k times · Source

I have a button of which when I click it I want to alert the background-image URL of #div1.

Is it possible?

Answer

Blazemonger picture Blazemonger · Jan 10, 2012

I usually prefer .replace() to regular expressions when possible, since it's often easier to read: http://jsfiddle.net/mblase75/z2jKA/2

    $("div").click(function() {
        var bg = $(this).css('background-image');
        bg = bg.replace('url(','').replace(')','').replace(/\"/gi, "");
        alert(bg);
    });