html - not able to load contents in div using javascript -
this shan , i'm javascript noob , i'm trying work qa code example here. i'm trying load small javascript content div element not working great , here code.
<html> <head> <title> using d statement </title> <script> function displaytext () { var loopindex=0; var sum=0; (var loopindex=1; loopindex <=100; loopindex++) { sum +=loopindex; }; document.getelementbyid('targetdiv').innerhtml="adding 1 100 gives "+sum; } </script> </head> <body> <div id="targetdiv"> </div> </body> </html>
you need call function. it's idea wait until window loaded (or can use more advanced js detect dom ready state.):
<html> <head> <title> using d statement </title> <script> function displaytext() { var loopindex=0; var sum=0; (var loopindex=1; loopindex <=100; loopindex++) { sum +=loopindex; }; document.getelementbyid('targetdiv').innerhtml = "adding 1 100 gives "+sum; } window.onload = function(){ displaytext(); } </script> </head> <body> <div id="targetdiv"> </div> </body> </html>
Comments
Post a Comment