2
<script type="text/javascript" language="javascript">
 function addNewRow()
 {
    var table = document.getElementById("table1");
    var tr = table.insertRow();
    var td = tr.insertCell();
    td.innerHTML= "a";

    td = tr.insertCell();
    td.innerHTML= "b";
    
    td = tr.insertCell();
    td.innerHTML= "c";
    
    td = tr.insertCell();
    td.innerHTML= "d";
    
    td = tr.insertCell();
    td.innerHTML= "e";
 }
</script>
<body>
    <table id="table1" border="1" cellpadding="0" cellspacing="0" width="100%">
        <tr id="row1">
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
    </table>
    <input type="button" onClick="addNewRow()" value="Add New"/>
</body>

This example is for dynamically insert new row and cells in the table. But its behavior is different in all browsers.

  1. Internet Explorer = It add row in the last and new added cells are starts from first.

  2. Chrome/Safari = It add new row in the first and new added cells are starts from end.

  3. Mozilla Firefox = It is not working.

    I want new added row in the last and new added cells starts from first like (Internet Explorer) in all browsers.

If you have any solution for same behavior please tell me.

Community
  • 1
  • 1
Karandeep Singh
  • 1,223
  • 5
  • 22
  • 34
  • So far, Stonedecroze's answer is most correct. You could spend weeks trying to get this to work on 3 or 4 browsers. Or you could follow the hyperlink and have a jQuery version running in a few minutes -- with much better assurance that it will work on as many browsers as possible. – Brock Adams Jun 16 '10 at 08:20
  • You might find this useful: [http://stackoverflow.com/questions/171027/add-table-row-in-jquery][1] [1]: http://stackoverflow.com/questions/171027/add-table-row-in-jquery – Silver Gonzales Jul 24 '12 at 18:17

1 Answers1

3

try to use:

var tr = table.insertRow(-1);

like its said here:

https://developer.mozilla.org/en/DOM/table.insertRow

Adam Lukaszczyk
  • 4,898
  • 3
  • 22
  • 22