I'm trying to build a JavaScript-based Layout grid generator where I want to control the number of grid rows by the value of an input field. E.g. if I type in 12
, my container-div
should have 12 divs within. I already made a function that can handle this...
document.getElementById('numofrows').onkeyup=function(){
var foo = [];
for (var i=0; i<this.value ;i++) {
foo[i] = document.createElement('div');
container.appendChild(foo[i]);
}
}
...But its drawback is, that every further input I make will add a number of divs again. How can I restrict the total number of child divs to what is just the actual value of the input field?