$.css()
The above function (jQuery.css()) returns the CSS value for a given property; in your case a string. Strings don't have a .height() method though.
So on your line..
var contentHeight = $(this).css('background-image').height();
This is actually happens..
$(this)
is evaluated; and returns a jQuery instance.
.css('background-image')
is ran on the jQuery instance, and a string is returned (the image name)
.height()
is ran on the string... but it doesn't exist.. uh oh.
In your scenario though, the height of the element itself will probably suffice.
$(this).height()
Especially considering that by default the background will just repeat if it's smaller than the element, and won't overflow if it's bigger.
Going beyond the question though, and to clarify after your comment below:
You want the size of the background image? This doesn't hold much use in reality. Take a look at the W3 documentation for background properties.
If the image is smaller than the element it's applied to, then the background-repeat
property defaults to repeat
. End result? The background takes on the whole element.
If the image is larger than the element it's applied to, then it's simply invisible outside the boundaries of the element. This is why sprites work. End result? The background takes on the whole element.
This behaviour can be changed using different properties, but in your scenario it doesn't appear to be. So essentially, the height of the element is the height of the background.
If you want to get the actual dimensions of the image then you're going to need to use a <img>
element which, being a DOM element, DOES have height()
and width()
methods/properties. (Although this is going to require absolute positioning and possibly some z-indexing; and if you need to do this then it's probably best to re-think whatever you're doing)