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

  • Home
  • SEARCH
  • 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 9131715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:11:13+00:00 2026-06-17T08:11:13+00:00

I am playing around with the JavaScript functions insertAfter and insertBefore , however I

  • 0

I am playing around with the JavaScript functions insertAfter and insertBefore, however I am trying to insertAfter and insertBefore two elements.

For instance, consider the following HTML:

<table>
    <tbody>
        <tr>
            <td>
                Item 1
            </td>
            <td>
                <div class="moveUpDown">
                    <div class="up">
                    </div>
                    <div class="down">
                    </div>
                    <div>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <table>
                    <tr>
                        <td>
                            1
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                Item 2
            </td>
            <td>
                <div class="moveUpDown">
                    <div class="up">
                    </div>
                    <div class="down">
                    </div>
                    <div>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <table>
                    <tr>
                        <td>
                            1
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
</table>

Then I have this JavaScript code snippet:

var row = $(this).parents("tr:first");
if ($(this).is(".up")) {
    row.insertBefore(row.prev());
} else {
    row.insertAfter(row.next());
}

Basically when the Up class is called, the previous row is moved up and when the Down class is called, the current row is moved down one row.

What I want to do, is move the rows Up/Down 2 rows… meaning something like row.prev().prev() or row.next().next() however this does not seem to work.

Is there an easy way around this?

Would appreciate any help/suggestions.

  • 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-17T08:11:14+00:00Added an answer on June 17, 2026 at 8:11 am

    to go up

    row.prev().prev().before(row)
    

    to go down

    row.next().next().after(row)
    

    obviously the tr must exist prev/next have to exist

    NB you are caching row as the first tr element so row is changing every time the first tr element change

    listen to event

    $("table").on("click","tr > td > span.moveup", function() {
      var row = $(this).parent().parent();
    
     if (row.prev().prev().get(0)) row.prev().prev().before(row)
    
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Been playing around with JavaScript, and what Im trying to do is only allow
I am new to HTML and javascript. Was playing around with it and faced
I've been playing around with prototypal inheritance after reading http://javascript.crockford.com/prototypal.html and having a bit
I am trying to write some google map functionlity and playing around with javascript
So I'm playing around with JavaScript and came across what I think to be
I've been playing around with JavaScript extending its functionality but have noticed some behaviour
I was playing around with some Javascript snippets today and noticed that this code
I was playing around with javascript prototype chain inheritance and i came accross this
Im playing around with the Tablesorter plug-in for jQuery and was trying to get
I’m playing around with json objects in javascript and wanted see if someone could

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.