Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9297425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:04:46+00:00 2026-06-18T22:04:46+00:00

I managed to fix some trouble I was having with traversing through table rows

  • 0

I managed to fix some trouble I was having with traversing through table rows with DOM using only JavaScript but ran into 2 hurdles. I’m trying to create a table, where each row will have a set of buttons to move that particular row up, down or remove it. I was able to successfully use the .replaceChild method but it replaces the row instead of just swapping them. When I tried .moveRow, I keep getting an error saying the HTML table section does not have that method. I run into the same problem when trying to swap the current row with the row below. Any suggestions?

function up(x){

        // var tableBody = document.getElementsByTagName("tbody")[0]; // tried it with tableBody and it still didn't work
        var table = x.parentNode.parentNode.parentNode;
        var tableRow = x.parentNode.parentNode.cloneNode(true);
        var previousRow = x.parentNode.parentNode.previousSibling.cloneNode(true);
        table.replaceChild(tableRow,x.parentNode.parentNode.previousSibling);

    }

    function down(x){
        var table = x.parentNode.parentNode.parentNode;
        var tableRow = x.parentNote.parentNode.cloneNode(true);
        var belowRow = x.parentNode.parentNode.nextSibling.cloneNode(true);
        table.moveRow(tableRow,x.parentNode.parentNode.nextSibling);
    }

My buttons:

<table id="table1" border="1">
        <tbody>
            <tr>
                <th>Column 1</th> <th>Column 2</th> <th>Column 3</th> <th>Column 4</th> 
            </tr>   
            <tr id="enterData">
                <td id="buttons">
                    <input type="button" value="Up" onclick="up(this)" />
                    <input type="button" value="Down" onclick="down(this)" /> 
                </td>
            </tr>
        </tbody>
        </table>    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T22:04:47+00:00Added an answer on June 18, 2026 at 10:04 pm

    You can use insertBefore to move up or down the rows and appendChild for the last row, also I use *ElementSibling to avoid text node issues but that might cause compatibily issues.

    function up(x){
        var row = x.parentNode.parentNode;
        var table = row.parentNode;
        //Don't move up over the header
        if (row.previousElementSibling && row.previousElementSibling.previousElementSibling){
            table.insertBefore(row,row.previousElementSibling);
        }
    }
    function down(x){
        var row = x.parentNode.parentNode;
        var table = row.parentNode;
        //can't use insertBefore for last row.
        if (row.nextElementSibling && row.nextElementSibling.nextElementSibling){
            table.insertBefore(row,row.nextElementSibling.nextElementSibling);
        }
        else{
            table.appendChild(row);
        }
    }
    

    DEMO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've no idea why, but on occasion I've managed to fix some compile errors,
I've managed to walk around this problem, but being only a javascript dabbler I
I managed to create a dynamic three dropdown list using Javascript code and it
I am implementing an application managed login mechanism using some primefaces components. I successfully
I managed to request the publish_actions permission, but since it is revocable from the
I managed to found out how to delete field but I have a problem
I managed to get some help from a previous question and I have the
I managed to get the YUI calendar widget working fine. But When it displays
Some of our non-technical users are having problems where a dialog MessageBox in our
I'm having some problems with Behat and Mink. The driver used is Goutte. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.