I find it hard to create a title to this question but what I really want to know is how to make my code (that creates tables derived from the value of an input) stop from stacking up tables every time an event is made. like when I input "2" it creates 2 tables but when I input "3" it adds 3 tables thus, 5 tables are made instead of just 3.
I want want my code to create tables derived from the input box rather than stacking it up. How can I achieve this?
<html>
<body>
<input type="text" onblur="myfunction()" onchange="myfunction" id="dino">
<br>
<table id="mytable">
<script>
function myfunction() {
var value = document.getElementById("dino").value;
for (var i = 0; i < value; i++){
var tr = document.createElement('tr');
var td1 = document.createElement('td');
var td2 = document.createElement('td');
var td3 = document.createElement('td');
var var_input_days = document.createElement("INPUT");
var_input_days.setAttribute("type", "text");
var_input_days.setAttribute("id", "ID_DAYS_" + [i]);
var_input_days.setAttribute("value","DAYS_" + [i]);
var_input_days.setAttribute("name", "DAYS_" + [i]);
var var_input_name = document.createElement("INPUT");
var_input_name.setAttribute("type", "text");
var_input_name.setAttribute("id", "ID_NAME_" + [i]);
var_input_name.setAttribute("value","NAME_" + [i]);
var_input_name.setAttribute("name", "NAME_" + [i]);
var var_input_data_ca = document.createElement("INPUT");
var_input_data_ca.setAttribute("type", "text");
var_input_data_ca.setAttribute("id", "ID_DATA_CA_" + [i]);
var_input_data_ca.setAttribute("value","ID_DATA_CA_" + [i]);
var_input_data_ca.setAttribute("name", "DATA_" + [i]);
td1.appendChild(var_input_name);
td2.appendChild(var_input_days);
td3.appendChild(var_input_data_ca);
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
document.getElementById("mytable").appendChild(tr);
}
document.body.appendChild(table);
}
</script>
</table>
</body>
</html>