You are here: Reference > JavaScript > client-side > HTML DOM > methods > moveRow (table, tbody, tfoot, thead)

moveRow method (table, tbody, tfoot, thead)

Browser support:
Moves a row to a new position in the current table, thead, tfoot or tbody element.

Syntax:

object.moveRow (fromIndex, toIndex);
You can find the related objects in the Supported by objects section below.

Parameters:

fromIndex
Required. Zero-based integer that specifies the position of the row to be moved in the rows collection of the current element. The value of -1 can also be used; in that case, the last row will be moved.
toIndex
Required. Zero-based integer that specifies the new position of the moved row in the rows collection of the current element. The value of -1 can also be used; in that case, the row will be moved to the last position.

Return value:

Returns the moved row element.

Example HTML code 1:

This example illustrates the use of the moveRow method:
<head>
    <script type="text/javascript">
        function SwapRows () {
            var table = document.getElementById ("table");
            if (table.moveRow) {        // Internet Explorer
                table.moveRow (0, 1);
            } 
            else {        // Cross browser
                var firstRow = tbody.rows[0];
                var secondRow = tbody.rows[1];
                firstRow.parentNode.insertBefore (secondRow, firstRow);
            }
        }
    </script>
</head>
<body>
    <button onclick="SwapRows ();">Swap rows!</button>
    <table id="table" border="1px">
        <tr>
            <td>First row.</td>
        </tr>
        <tr>
            <td>Second row.</td>
        </tr>
    </table>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content