I would change the name of the headerElem variable too. That name clearly
indicates that it's supposed to be a DOM element, but it isn't. This will
lead to much confusion.
$() does not return a DOM element, it returns a jQuery object, which is an
array of DOM elements (even in the case where there is only one).
Merc, one convention you'll find in a lot of jQuery code is to use $ at the
beginning of a variable that contains a jQuery object:
var $header = $('#header');
var headerHeight = $header.height();
This gives you a visual reminder that the variable is a jQuery object, since
it looks like the $ that was used to create the object in the first place.
If you do want to get to the actual DOM element(s) inside a jQuery object,
use array indexing. In the case of a #id selector, there is only a single
element and you can use [0]:
var $header = $('#header');
var headerElement = $header[0];
var headerHeight = headerElement.offsetHeight;
-Mike
_____
From: Karl Swedberg
Hi Merc,
If you want an integer, try this:
var headerElem = $("#header");
var headerHeight = headerElem.height();
If you want the value with "px", try this:
var headerElem = $("#header");
var headerHeight = headerElem.css('height');
Hope that helps.
--Karl
_________________
Karl Swedberg
www.englishrules.com
www.learningjquery.com
On Oct 9, 2007, at 1:02 AM, Merc70 wrote:
I'm new to Javascript and jQuery, so I'm sorry if this is basic
stuff. How do I access offsetHeight for an element? This is what I
have, but offsetHeight comes up undefined:
var headerElem = $("#header");
var headerHeight = headerElem.offsetHeight;
whereas this works:
var docHeader = document.getElementById("header");
headerHeight = docHeader.offsetHeight;
Thanks --MERC