Debsish Ghosh
Debsish Ghosh

Reputation: 35

Add row with Inputted value

I Add/Remove dynamic rows in HTML table. With this code I can dynamically add and remove rows. I want to add a row with an input value in the 1st row as if I input 'debasish' it will append next row with value 'debasish'. But with append row with blank value. How can I append the row with the input value?

<html>
<head>
<title>Add/Remove dynamic rows in HTML table</title>
<script>
        function addRow(tableID) {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);
            var colCount = table.rows[0].cells.length;
            for(var i=0; i<colCount; i++) {
                var newcell = row.insertCell(i);
                newcell.innerHTML = table.rows[1].cells[i].innerHTML;
                //alert(newcell.childNodes);
                switch(newcell.childNodes[0].type) {
                    case "text":
                            newcell.childNodes[0].value = "";
                            break;
                    case "checkbox":
                            newcell.childNodes[0].checked = false;
                            break;
                    case "select-one":
                            newcell.childNodes[0].selectedIndex = 0;
                            break;
                }
            }
            clearForm();
        }
        function deleteRow(tableID) {
            try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;

            for(var i=0; i<rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[0].childNodes[0];
                if(null != chkbox && true == chkbox.checked) {
                    if(rowCount<=1) {
                        alert("Cannot delete all the rows.");
                        break;
                    }
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }
            }
            }catch(e) {
                alert(e);
            }
        }
    </script>
</head>
<body>
    <form action="#" method="post">
     <input type="button" value="Add Row" onclick="addRow('dataTable')" />
        <input type="button" value="Delete Row"
            onclick="deleteRow('dataTable')" />
        <table id="dataTable" width="550px" border="1">
            <tbody>
            <thead>
              <tr> 
              <th>Select </th>
               <th>Name</th>
               </tr>
            </thead>
            <tr>
                <td><input type="checkbox" name="chk" /></td>
                <td><input type="text"  name="name"></td>
            </tr>
            </tbody>
        </table>
    </form>
</body>
</html>

Upvotes: 1

Views: 554

Answers (1)

If you only want to get the value from the input in the first row, use the following:

 case "text":
    newcell.childNodes[0].value = table.rows[1].cells[1].querySelector("input").value;
    break;

Demo

function addRow(tableID) {
  var table = document.getElementById(tableID);
  var rowCount = table.rows.length;
  var row = table.insertRow(rowCount);
  var colCount = table.rows[0].cells.length;
  for (var i = 0; i < colCount; i++) {
    var newcell = row.insertCell(i);
    newcell.innerHTML = table.rows[1].cells[i].innerHTML;
    //alert(newcell.childNodes);
    switch (newcell.childNodes[0].type) {
      case "text":
        newcell.childNodes[0].value = table.rows[1].cells[1].querySelector("input").value;
        break;
      case "checkbox":
        newcell.childNodes[0].checked = false;
        break;
      case "select-one":
        newcell.childNodes[0].selectedIndex = 0;
        break;
    }
  }
  //clearForm();
}

function deleteRow(tableID) {
  try {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;

    for (var i = 0; i < rowCount; i++) {
      var row = table.rows[i];
      var chkbox = row.cells[0].childNodes[0];
      if (null != chkbox && true == chkbox.checked) {
        if (rowCount <= 1) {
          alert("Cannot delete all the rows.");
          break;
        }
        table.deleteRow(i);
        rowCount--;
        i--;
      }
    }
  } catch (e) {
    alert(e);
  }
}
<form action="#" method="post">
  <input type="button" value="Add Row" onclick="addRow('dataTable')" />
  <input type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
  <table id="dataTable" width="550px" border="1">
    <tbody>
      <thead>
        <tr>
          <th>Select </th>
          <th>Name</th>
        </tr>
      </thead>
      <tr>
        <td><input type="checkbox" name="chk" /></td>
        <td><input type="text" name="name"></td>
      </tr>
    </tbody>
  </table>
</form>

Upvotes: 2

Related Questions