javascript - Tables are stacking instead of replacing -


i find hard create title question want know how make code (that creates tables derived value of input) stop stacking tables every time event made. when input "2" creates 2 tables when input "3" adds 3 tables thus, 5 tables made instead of 3.

i want want code create tables derived input box rather stacking up. how can 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;   (var = 0; < 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> 

what doing adding more elements added ones. need first clear contents of table , recreate it. can achieve adding 1 line @ beginning of myfunction()

document.getelementbyid("mytable").innerhtml=""; 

this clear contents of mytable before recreating it.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -