0

I created a table, on referring "http://stackoverflow.com/questions/9045940/dynamically-adding-table-row" , whenever i came into the last row in the table i need to add another row into the table at the bottom dynamically, here is the code i tried:

<table id="myTable" style="table-layout:auto">

   <tr>
   <td><input type="text" onblur="addRow('myTable')" /></td>
    <td><input type="text"/></td>
    <td><input type="text"/></td>
  </tr>

</table>

and the javascript is as follows:

<script>
function addRow()
{
   //add a row to the rows collection and get a reference to the newly added row
   var newRow = document.all("myTable").insertRow();

   var oCell = newRow.insertCell();
   oCell.innerHTML = "<input type='text' onblur='addRow();'>";

   oCell = newRow.insertCell();
   oCell.innerHTML = "<input type='text' name='title'>";

   oCell = newRow.insertCell();
   oCell.innerHTML = "<input type='text' name='title'>";   
}

</script>

But, here a new row is added at the top of the table, what should i do?

1 Answer 1

2

change var newRow = document.all("myTable").insertRow();

to

var newRow = document.all("myTable").insertRow(-1);

Also, in your onblur event, why do you pass an argument to the addRow function when it doesn't take any?

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

8 Comments

here one more problem i got is that whenever i inserted the value in the last row an additional row is inserted but here what the thing is once an additional row is inserted then the new row is the last one but not the previous one so next time when i enter into the last row only a new row should add how can we do that??
can u please tell me what can i do to rectify this?
it might be best just to add a button next to each row and you can click it when you want to add another row.. because no matter what, any time you change focus from the last row it will make another.
no in my case i should not add any buttons unfortunately....so when there is already a new row the previous onblur should not work, is there any way we can do like that??
If you did that, then you could only add one additional row.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.