2009
12.10

Sometimes when you need to get elements position like top and left,  or width and height, they always return zero value, or lesser to what we expected. Sometimes this problem occurred to elements display value.  So for quick fix, i used this code.

jQuery

var width = $(‘#hiddendiv’).css({display:”",visibility:”hidden”}).innerWidth();

$(‘#hiddendiv’).css({display:”none”,visibility:”visible”});

Javascript

var obj = document.getElementById(‘hiddendiv’);

obj.style.visibility=”hidden”; obj.style.display=”";

var width = obj.clientWidth;

obj.style.display=”none”; obj.style.visibility=”visible”;

Explanation : display:”none” means nothing to display, nor width, height, top or left. So to get those attributes without showing the element I used visibility:”hidden” and display:”". Then after getting the attributes I should return the visibility to “visible” and display to “none” because unlike display, visibility leaves blank spaces.

The clientWidth, innerWidth, top, left are returning 0 value

Comments are closed.