0

The console doesn't give any error, and the window displays nothing. What is wrong with it?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Home</title>
    <script type="text/javascript">
        function stuffify(){
            for(var a=6;a<=0;a--){
                document.getElementById("stuff").innerHTML+="<h"+a+">stuff</h"+a+"></br>";
            }
        }
    </script>
</head>
<body>
    <div id="stuff" onload="stuffify()"></div>

</body>
</html>
2
  • 2
    div elements don't trigger a load event. The function is never called. Commented Mar 12, 2013 at 1:44
  • Also, look at the condition on your loop. It will never be entered, it should be a>0 (it starts at 6, which is greater than zero, so the loop body never runs). Commented Mar 12, 2013 at 1:52

3 Answers 3

2

The onload function will work when on the body tag of your document.

Also, the condition of your for loop is incorrect, it should be: for(var a=6; a >= 0; a--)

Sign up to request clarification or add additional context in comments.

2 Comments

@eicto I've updated by statement to be more correct for the problem at hand.
@Calvin Koder I've updated my answer to indicate why your code would not work even when moving the event to the button you added.
0

try this

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Home</title>
    <script type="text/javascript">
        function stuffify(){
            for(var a=6;a<=0;a--){
                document.getElementById("stuff").innerHTML+="<h"+a+">stuff</h"+a+"></br>";
            }
        }

    </script>
</head>
<body onload="stuffify();">
    <div id="stuff" ></div>

</body>
</html>

Comments

0

onload is supported by following tags:

<body>, <frame>, <frameset>, <iframe>, <img>, <input type="image">, <link>, <script>, <style>  

This explains why stuffify is not triggered after your div is loaded.
So, you can bind the onload function to body instead of div, or insert script after div, like this:

<div id="stuff" ></div>
<script type="text/javascript">
stuffify();
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.