PDA

View Full Version : how to find div width at page load......



inderjeetsinghsethi
12-06-2010, 06:19 AM
hey frnds,

m new learner of js and jquery......
i have at html structure like above......

<div style="width:965px">
<div style="width:100%" align="center">
<div style="width:365px" align="center">
<div id="test">

</div>
</div>
</div>
</div>

now what i want...
i want to find the div "test" width at page load event (<body onload="findwidth();")..
result should be 365 px....
but when i m using
var width=document.getelementbyid('test').offsetWidth;
than it show the undefined in width...

any help.....

Inderjeet Singh Khalsa:o

jscheuer1
12-06-2010, 06:55 AM
document.getelementbyid('test')

is not a function. I think you want:


document.getElementById('test')

Anyways, this works just fine:


<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function findwidth(){
var width = document.getElementById('test').offsetWidth;
alert(width);
}
</script>
</head>
<body onload="findwidth();">
<div style="width:965px">
<div style="width:100%" align="center">
<div style="width:365px" align="center">
<div id="test">

</div>
</div>
</div>
</div>
</body>
</html>