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>
You have defined a function named gengrid but are not running it. Below the definition of the function, try putting gengrid();.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With