Jump to content

I'm trying to get a table to output numbers starting at 5 then add 7 till i printed 20 numbers. My teacher told me to keep the JavaScript in the head. I think my problem is i don't know how to get it to print out correctly. I even looked at one of his examples. Here is my code

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
        <style type = "text/css">
        
        </style>
        <script type= "text/javascript">
        //<![CDATA[
        var seven = 7;
        var number = 5;
        var tbl = "<table>";
         for (var i = 0;i < 20;i++){
         tbl += "<tr><td>document.write("number")</td></tr>";
         number += seven;
         }
         tbl += "</table>";
         
        document.getElementById('Numbers').innerHTML = tbl;
        //]]>
        </script>
    </head>

    
    <body>
        <div id = "Numbers">

        </div>
 

    </body>
    

Link to comment
https://linustechtips.com/topic/674499-help-html-javascript/
Share on other sites

Link to post
Share on other sites

4 hours ago, DarkWolf1801 said:

I'm trying to get a table to output numbers starting at 5 then add 7 till i printed 20 numbers. My teacher told me to keep the JavaScript in the head. I think my problem is i don't know how to get it to print out correctly. I even looked at one of his examples. Here is my code

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
        <style type = "text/css">
        
        </style>
        <script type= "text/javascript">
        //<![CDATA[
        var seven = 7;
        var number = 5;
        var tbl = "<table>";
         for (var i = 0;i < 20;i++){
         tbl += "<tr><td>document.write("number")</td></tr>";
         number += seven;
         }
         tbl += "</table>";
         
        document.getElementById('Numbers').innerHTML = tbl;
        //]]>
        </script>
    </head>

    
    <body>
        <div id = "Numbers">

        </div>
 

    </body>
    

So your Javascript is executed before the DOM has loaded! Easiest solution, which works across all browsers: Put your Javascript just before the Body ends.

If this is not an alternativem you could use something like this:

document.addEventListener("DOMContentLoaded", function(event) { 
  	//do your Stuff here!
	//doesn't work in IE8
});

 

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to comment
https://linustechtips.com/topic/674499-help-html-javascript/#findComment-8687415
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×