Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
85
rated 0 times [  88] [ 3]  / answers: 1 / hits: 43020  / 14 Years ago, thu, october 21, 2010, 12:00:00

I cannot seem to get this script to work. Can anyone please help? The DIV's width is not defined. It just stretches across the whole page.



<html>
<head>
<style type=text/css>
#box{
height:100px;
border:3px;
border-style:solid;
border-color:#000;
}
</style>
<script>
document.getElementById('box').style.width=10px;
</script>
</head>
<body>
<div id=box></div>

More From » css

 Answers
37

The element does not exist on the page yet. JavaScript can not access/manipulate an element until it has been loaded in the DOM. You can overcome this by moving you <script> block to above the closing </body>. Or use an window.load event.



An example of the former using your code is here - http://jsfiddle.net/ycWxH/


[#95231] Tuesday, October 19, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dasias

Total Points: 344
Total Questions: 100
Total Answers: 100

Location: Hong Kong
Member since Tue, Oct 19, 2021
3 Years ago
;