Search code examples
javascriptinternet-explorergoogle-chromeinsertcell

InsertCell() difference in Chrome and IE


I'm adding cells into a table in a for loop but result is different in ie and chrome. Cells are in opposite order.

Such as;

IE [[cell_1][cell_2][cell_3][cell_4]]

Chrome [[cell_4][cell_3][cell_2][cell_1]]

Why would such problem happen, and is there a way to add cells with some other methods (maybe jQuery)?

I use ie 10.0.9 and chrome 30.0.1


Update:

You can try it with this code in both browsers;

<html>
<head>
    <script type="text/javascript">
        function fillTable(){
            var aTBL = document.getElementById("table");

            aTR = aTBL.insertRow();

            for(i = 0; i < 5; i++){
                aTD = aTR.insertCell();
                aTD.innerHTML = "[cell_" + i + "]";
            }
        }
    </script>   
</head>
<body>
    <table id="table">
    </table>

    <script type="text/javascript">
        fillTable();
    </script>
</body>
</html>

Solution

  • Looks like browsers have different default values, where to insert the cell, if you're not passing an argument to insertCell(). Pass -1 as an argument, then the cell is appended at the end of a row.

    aTD = aTR.insertCell(-1);