0

I am trying to add an empty row based on the row that I already have. I have spent an hour looking around and trying different ways but none seem to work. This is what I have so far:

HTML:

<div id="Oretablediv">
<table id="OreTable" border="1">
    <tr>
        <td>Ore</td>
        <td>Amount</td>
        <td>Price</td>
        <td>Sub Total</td>
        <td>Delete?</td>
        <td>Add Rows?</td>
    </tr>
    <tr>
      <td>
        <select name="Ore">
          <option value="value1">Value 1</option>
          <option value="value2">Value 2</option>
        </select>
      </td>
      <td><input size=25 type="text" id="Amount" ></td>
      <td>Price</td>
      <td>SubTotal</td>
      <td><input type="button" id="delOrebutton" value="Delete" onclick="deleteRow(this)"/></td>
      <td><input type="button" id="addOrebutton" value="Add More Ore" onclick="insRow()"/></td>
    </tr>
</table>

Javascript:

function deleteRow(row){
var i=row.parentNode.parentNode.rowIndex;
document.getElementById('OreTable').deleteRow(i);
}

function insRow(){
  var row = document.createElement('tr');
  var col = document.createElement('td');
  var col2 = document.createElement('td');
  row.appendChild(col);
  row.appendChild(col2);
  var table = document.getElementById("OreTable");
  table.appendChild(row);
}  

I am currently only asking about the insRow() function, if you have suggestions for the delete row please share.

3
  • I’m not sure what you need. Do you want to clone the row where you clicked “Add More Ore” with all the inputs filled out like the row to be cloned? Then try cloneNode and then fill out the fields after that. Commented Mar 29, 2016 at 3:55
  • 1
    What is not working for you.. What's the problem Commented Mar 29, 2016 at 3:58
  • link check this Commented Mar 29, 2016 at 4:03

3 Answers 3

3

As you are creating copy of the existing tr element, you can achieve this using cloneNode instead of creating all the DOM elements using createElement

Try this:

function deleteRow(row) {
  var i = row.parentNode.parentNode.rowIndex;
  document.getElementById('OreTable').deleteRow(i);
}

function insRow() {
  var tr = document.getElementById('toClone');
  var cln = tr.cloneNode(true);
  var elem = cln.querySelector('#Amount');
  elem.value = '';
  var table = document.getElementById("OreTable");
  table.appendChild(cln);
}
<div id="Oretablediv">
  <table id="OreTable" border="1">
    <tr>
      <td>Ore</td>
      <td>Amount</td>
      <td>Price</td>
      <td>Sub Total</td>
      <td>Delete?</td>
      <td>Add Rows?</td>
    </tr>
    <tr id='toClone'>
      <td>
        <select name="Ore">
          <option value="value1">Value 1</option>
          <option value="value2">Value 2</option>
        </select>
      </td>
      <td>
        <input size=25 type="text" id="Amount">
      </td>
      <td>Price</td>
      <td>SubTotal</td>
      <td>
        <input type="button" id="delOrebutton" value="Delete" onclick="deleteRow(this)" />
      </td>
      <td>
        <input type="button" id="addOrebutton" value="Add More Ore" onclick="insRow()" />
      </td>
    </tr>
  </table>

Fiddle here

Sign up to request clarification or add additional context in comments.

Comments

1

You can attach the click to the button id as follows:

Here is the fiddle: https://jsfiddle.net/swaprks/c1wsomxp/

JAVASCRIPT:

document.getElementById("delOrebutton").onclick = function eleteRow(row){
    var i= this.parentNode.parentNode.rowIndex;
    document.getElementById('OreTable').deleteRow(i+1);
}

document.getElementById("addOrebutton").onclick = function insRow(){
  var row = document.createElement('tr');
  var col = document.createElement('td');
  var col2 = document.createElement('td');
  row.appendChild(col);
  row.appendChild(col2);
  var table = document.getElementById("OreTable");
  table.appendChild(row);
} 



HTML:

<div id="Oretablediv">
<table id="OreTable" border="1">
    <tr>
        <td>Ore</td>
        <td>Amount</td>
        <td>Price</td>
        <td>Sub Total</td>
        <td>Delete?</td>
        <td>Add Rows?</td>
    </tr>
    <tr>
      <td>
        <select name="Ore">
          <option value="value1">Value 1</option>
          <option value="value2">Value 2</option>
        </select>
      </td>
      <td><input size=25 type="text" id="Amount" ></td>
      <td>Price</td>
      <td>SubTotal</td>
      <td><input type="button" id="delOrebutton" value="Delete" /></td>
      <td><input type="button" id="addOrebutton" value="Add More Ore"/></td>
    </tr>
</table>
</div>

I have also updated the delete function to delete the first row which was added.

Comments

0

It looks like your script will insert the row as a direct child of the "table" tag. Browsers will often insert a "tbody" tag as a child of the table tag, but a parent to the "tr" tags. So I think your appendChild(row) call should use the tbody element instead of the table element.

2 Comments

That’s what I also thought first, but upon testing it, it seemed to work just fine.
My guess is OP is expecting td elements having input elements in it..But he is not appending/cloning those elements anywhere..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.