Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clientHeight returns zero

I'm creating the DOM elements dynamically using JavaScript. I need to get height of one div to do the slide down animation. But when i use clientHeight its always returning a 0. offsetHeight also do the same. I'm getting the div when i tried console.log(). So its not because the element is not loaded in DOM. Initially i set style display:none for the div to hide it. Is this the reason for the problem? Please someone help me.

document.getElementsByClassName('myDiv')[0].parentNode.children[1].clientHeight;

I tried the below code to avoid the problem of display:none. Still clientHeight returns 0.

if(obj.style.display == "none"){ // if it's allready hidden we slide it down
   obj.style.visibility = "hidden";
   obj.style.display = "block";
   height = obj.offsetHeight;
   obj.style.visibility = "visible";
   slideDown(obj,0,height,Math.ceil(height/timeToSlide));
} 
like image 989
dittops Avatar asked Apr 09 '14 19:04

dittops


1 Answers

Elements styled with display:none will register 0 for its dimensions. Try applying this instead:

div.myClass { position:absolute; left:-999em; }

Same effect, but your div will still have its dimensions.

like image 184
Mister Epic Avatar answered Sep 23 '22 03:09

Mister Epic