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>
gengrid()elsewhere?id's are not allowed to start with a number. Thank may be what is causing your problem, but even if it isn't, you need to change that.srcattribute of yourimgtag. Furthermore theimgtag should be self-closing, i.e.<img src="whatever.jpg" alt="a" />.