javascript - JS Nodes in for loop -


i trying add many options in select tag.
code is:

var years = document.getelementbyid("years"); ( var = 1526; < 2013; i++) {     var year = document.createelement("option");     var text = document.createtextnode(i);     year.appendchild(text);     year.setattribute("value", i);     years.appendchild(year); } 

this doesn't work. there wrong appendchild function. loop breaks after running once. tried doing

var years = document.createelementnode("select"); 

but doesn't work. runs there nothing on webpage. no select tag , no options.

the body:

<select id="years"></select> 

i have used div tag in append child function

<html> <script> function loadoptions() {     var i;     var selectdiv= "";     selectdiv="<select>";     for(i = 1526; < 2013; i++){         selectdiv+= "<option value='"+i+"'>"+i+"</option>";     }         selectdiv+= "</select>";     var newdiv=document.createelement('div');     newdiv.innerhtml= selectdiv;     document.getelementbyid('element1').appendchild(newdiv); } </script> <body onload="loadoptions()">  <div id="element1"></div>  </body> </html> 

hope you


Comments