Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't my function running on document load as expected?

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!

like image 737
cmplieger Avatar asked Aug 06 '12 22:08

cmplieger


2 Answers

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

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

like image 114
Musa Avatar answered Nov 14 '22 22:11

Musa


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>     
like image 28
MKT Avatar answered Nov 14 '22 21:11

MKT