It should just be $('#someDiv').height(); with jQuery. This retrieves the height of the first item in the wrapped set as a number. Trying to use .style.
Height only works if you have set the property in the first place. Not very useful!
The OP is using jQuery, so using the jQuery height command just makes the most sense – Russ Cam Feb 8 '09 at 21:13 +1 from me. I think I gave up on height() the other day because I was using height not height(). So I learned something.
– brunot Feb 8 '09 at 23:28 I did. I didn't see the part where the OP was using jQuery.My apologies. I can't remove the downvote until you edit your post, but I'll gladly it remove it if you edit it so it'll let me.
– Paolo Bergantino Feb 9 '09 at 2:09 removed and +1'ed for your troubles. :) – Paolo Bergantino Feb 9 '09 at 15:53 One word - awesome! Thanks for everyone's participation.
Lots of good info on this thread. This will allow me to center pages using CSS but use jQuery to make the overall height of the "container" div correct without venturing into CSS hackville. Thanks again.
– brunot Feb 9 '09 at 17:30.
Try one of: var h = document. GetElementById('someDiv'). ClientHeight; var h = document.
GetElementById('someDiv'). OffsetHeight; var h = document. GetElementById('someDiv').
ScrollHeight; clientHeight includes the height and vertical padding. OffsetHeight includes the height, vertical padding, and vertical borders. ScrollHeight includes the height of the contained document (would be greater than just height in case of scrolling), vertical padding, and vertical borders.
1 for adding value to the thread. – brunot Feb 15 '09 at 15:01.
Definitely use $('#someDiv').height() // to read it or $('#someDiv'). Height(newHeight) // to set it I'm posting this as an additional answer because theres a couple important things I just learnt. I almost fell into the trap just now of using offsetHeight.
This is what happened : I used the good old trick of using a debugger to 'watch' what properties my element has I saw which one has a value around the value I was expecting It was offsetHeight - so I used that. Then I realized it didn't work with a hidden DIV I tried hiding after calculating maxHeight but that looked clumsy - got in a mess. I did a search - discovered jQuery.height() - and used it found out height() works even on hidden elements just for fun I checked the jQuery implementation of height/width Here's just a portion of it : Math.
Max( Math. Max(document. Body"scroll" + name, document.
DocumentElement"scroll" + name), Math. Max(document. Body"offset" + name, document.
DocumentElement"offset" + name) ) Yup it looks at BOTH scroll and offset. If that fails it looks even further, taking into account browser and css compatibility issues. In other words STUFF I DONT CARE ABOUT - or want to.
But I don't have to. Thanks jQuery! Moral of the story : if jQuery has a method for something its probably for a good reason, likely related to compatibilty.
If you haven't read through the jQuery list of methods recently I suggest you take a look.
Saw your other comment at the question level. I'm sold on jQuery. Have been for a little while.
But this sealed the deal. I'm in love with the fact this simple thing is going to solve 100s of layout issues for me (mostly on Intranet sites where I know JavaScript is running). – brunot Feb 13 '09 at 5:40.
"if you need to calculate something but not show it, set the element to visibility:hidden and position:absolute, add it to the DOM tree, get the offsetHeight, and remove it. (That's what the prototype library does behind the lines last time I checked)." I have the same problem on a number of elements.
There is no JQuery or Prototype to be used on the site but I'm all in favor of borrowing the technique if it works. As an example of some things that failed to work (followed by what did,) I have the following code: // Layout Height Get function fnElementHeightMaxGet(DoScroll, DoBase, elementPassed, elementHeightDefault) { var DoOffset = true; if (!elementPassed) { return 0; } if (!elementPassed. Style) { return 0; } var thisHeight = 0; var heightBase = parseInt(elementPassed.style.
Height); var heightOffset = parseInt(elementPassed. OffsetHeight); var heightScroll = parseInt(elementPassed. ScrollHeight); var heightClient = parseInt(elementPassed.
ClientHeight); var heightNode = 0; var heightRects = 0; // if (DoBase) { if (heightBase > thisHeight) { thisHeight = heightBase; } } if (DoOffset) { if (heightOffset > thisHeight) { thisHeight = heightOffset; } } if (DoScroll) { if (heightScroll > thisHeight) { thisHeight = heightScroll; } } // if (thisHeight == 0) { thisHeight = heightClient; } // if (thisHeight == 0) { // Dom Add: // all else failed so use the protype approach... var elBodyTempContainer = document. GetElementById('BodyTempContainer'); elBodyTempContainer. AppendChild(elementPassed); heightNode = elBodyTempContainer.
ChildNodes0. OffsetHeight; elBodyTempContainer. RemoveChild(elementPassed); if (heightNode > thisHeight) { thisHeight = heightNode; } // // Bounding Rect: // Or this approach... var clientRects = elementPassed.getClientRects(); heightRects = clientRects.
Height; if (heightRects > thisHeight) { thisHeight = heightRects; } } // // Default height not appropriate here // if (thisHeight == 0) { thisHeight = elementHeightDefault; } // if (thisHeight > 3000) { // ERROR thisHeight = 3000; } return thisHeight; } // which basically tries anything and everything only to get a zero result. ClientHeight with no affect. With the problem elements I typically get NaN in the Base and zero in the Offset and Scroll heights.
I then tried the Add DOM solution and clientRects to see if it works here. 29 Jun 2011, I did indeed update the code to try both adding to DOM and clientHeight with better results than I expected.1) clientHeight was also 0.2) Dom actually gave me a height which was great.3) ClientRects returns a result almost identical to the DOM technique. Because the elements added are fluid in nature, when they are added to an otherwise empty DOM Temp element they are rendered according to the width of that container.
This get weird, because that is 30px shorter than it eventually ends up. I added a few snapshots to illustrate how the height is calculated differently. The height differences are obvious.
I could certainly add absolute positioning and hidden but I am sure that will have no effect. I continued to be convinced this would not work!(I digress further) The height comes out (renders) lower than the true rendered height. This could be addressed by setting the width of the DOM Temp element to match the existing parent and could be done fairly accurately in theory.
I also do not know what would result from removing them and adding them back into their existing location. As they arrived through an innerHTML technique I will be looking using this different approach. * HOWEVER * None of that was necessary.
In fact it worked as advertised and returned the correct height! When I was able to get the menus visible again amazingly DOM had returned the correct height per the fluid layout at the top of the page (279px). The above code also uses getClientRects which return 280px.
This is illustrated in the following snapshot (taken from Chrome once working. ) Now I have noooooo idea why that prototype trick works, but it seems to. Alternatively, getClientRects also works.
I suspect the cause of all this trouble with these particular elements was the use of innerHTML instead of appendChild, but that is pure speculation at this point. Regards, Dave Horsman.
OffsetHeight, usually. If you need to calculate something but not show it, set the element to visibility:hidden and position:absolute, add it to the DOM tree, get the offsetHeight, and remove it. (That's what the prototype library does behind the lines last time I checked).
Interesting. I've never checked prototype. Any idea why jQuery doesn't do that behind the scenes?
– Simon_Weaver Feb 13 '09 at 3:58.
If you haven't you need to use offsetHeight; rather than height var h = document. GetElementById('someDiv').style.offsetHeight.
OffsetHeight is a property of the element itself, not of its style. – Shog9? Feb 8 '09 at 21:07.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.