The problem is simple. How do I get a div's background image size (width and height) in jQuery. Is it even possible? I thought this would work:
jQuery('#myDiv').css('background-image').height();
The error message I get is that this is not a function.
I merged the answer from John together with the Plugin Style from Stryder. This plugin waits for the image to load:
feel free to extend this code: https://gist.github.com/1276118
Combined version of the above solutions
One more version. No DOM manipulation needed, all characters in image file name supported.
UPDATE See an alternative version below.
2018 solution
This answer is still receiving upvotes 5 years after. I thought I will share a more complete solution. This builds on top of original code and wraps it into a function. It uses jQuery Deferred Object, adds error handling and updates RegExp to match 3 possible cases:
url()
,url("")
andurl('')
. It also works on jQuery 1 to 3.In the case your css had double quotes or browser manipulation do the following.
I made the same thing for the width :)
the same as above, but using regexp instead of replace()