5

This code should set an elements height; however no style gets added. Am I missing something obvious?

function setGround() { 
    document.getElementById('content').style.height = '40px';
} 

document.onload = setGround; 

The HTML is quite basic:

<!DOCTYPE html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Template</title>
    <link rel="stylesheet" type="text/css" href="css/default.css"/>
    <link rel="stylesheet" type="text/css" href="css/orange.css"/>
    <script type="text/javascript" src="javascript/detect-css.js"></script>
</head>

<body>
    <header>
        <div id="logo"></div>
    </header>
    <section id="sidebar"><p>sf </p>
    </section>

    <section id="content"><p>sf </p>
    </section>

</body>
</html>

Thank you for your help!

isherwood
  • 46,000
  • 15
  • 100
  • 132
cmplieger
  • 6,375
  • 15
  • 49
  • 80
  • 1
    A very simple solution is to put your script down just before the `

    ` tag. Then you can just do `setGround()`.

    –  Aug 06 '12 at 22:22

4 Answers4

4

Don't use document.onload use window.onload instead.

See http://jsfiddle.net/mowglisanu/r6NzE/

Musa
  • 89,286
  • 16
  • 105
  • 123
2

you can use this:

function setGround() { 
    document.getElementById('content').style.height = '40px';
} 
document.onload = setGround;

but if you want see changes, you should create border on section tag by use this:

<section id="content" style='border:1px solid fuchsia;' >
<p>sf </p>
</section>     
MKT
  • 615
  • 1
  • 6
  • 18
1

You should use

window.onLoad = setGround; 

instead of

document.onload = setGround;
IshaniNet
  • 123
  • 10
0

Where have you placed the javascript-code? Is it in the detect-css.js?

This works:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Template</title>
<link rel="stylesheet" type="text/css" href="css/default.css"/>
<link rel="stylesheet" type="text/css" href="css/orange.css"/>
<script language="javascript">
function resize(){
document.getElementById("content").style.height='100px';
}
</script>
</head>

<body onload="resize()">
<header><div id="logo"></div></header>
<section id="sidebar"><p>sf </p>
</section>

<section id="content" style="background-color:#CCC; display:block;"><p>sf </p>
</section>

</body>
</html>
user1560066
  • 11
  • 1
  • 2