Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding multiple HTML elements in a javascript loop

I am trying to add multiple elements in a page through a loop in javascript but the code is not running can someone please point out what is wrong

<body>
<script type="text/javascript">
function gengrid()
      {
          var i=0;
          var num_stud=8;
          var newdiv;
          var divIdName;
          for(i=1;i<=num_stud;i++)
          {
             newdiv = document.createElement('div');
             divIdName = '50'+i;
             newdiv.setAttribute('id',divIdName);
             newdiv.innerHTML ='<img src=50'+i+'.jpg alt="a"></img>';
             document.body.appendChild(newdiv);
          }
      }
  </script>

like image 638
Abhishek Singh Avatar asked Oct 20 '25 21:10

Abhishek Singh


1 Answers

You have defined a function named gengrid but are not running it. Below the definition of the function, try putting gengrid();.

like image 196
Cymen Avatar answered Oct 23 '25 11:10

Cymen