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 6653223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:14:35+00:00 2026-05-26T01:14:35+00:00

I have table rows that move up and down, but my problem is that

  • 0

I have table rows that move up and down, but my problem is that the data table rows replace the table header (first row).

I want a fixed first row so that when you click the Up arrow you do not move the row up to replace the header.

I have tried some conditional logic to check if the current row is the table’s first row, but it is not working.

$(document).ready(function(){

<!--- Move: click to move rows up or down --->
$(".up,.down").click(function(){
var row = $(this).parents("tr:first");

<!-- this var does not work -->
var firstrow = $('table')[0].rows[0]; 

<!-- Check that is not the first row NEEDS FIXED -->
if (row != firstrow){

if ($(this).is(".up")) {
row.insertBefore(row.prev());
} else {
row.insertAfter(row.next());
}
}
});
});

<table>
<tr>
<td>Do Not Replace Me</td>
<td >&nbsp;</td>
</tr>
<tr>
<td>Two</td>
<td>
<a href="#" class="up">Up</a>
<a href="#" class="down">Down</a>
</td>
</tr>
<tr>
<td>Three</td>
<td>
<a href="#" class="up">Up</a>
<a href="#" class="down">Down</a>
</td>
</tr>
</table>

The table must stay as is, and I cannot change td to th or anything like that.

I am hoping there is just a way to fix those two lines of code.

  • 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-05-26T01:14:35+00:00Added an answer on May 26, 2026 at 1:14 am

    This would, as I suppose you know (given your disclaimer that the html must ‘stay as is…’), be much easier if you could put the header/first row into a thead element and the remainder into a tbody, but one way that works with the html ‘as is:’

    $(".up,.down").click(function() {
        var row = $(this).parents("tr:first");
    
        var firstrow = $('table tr:first');
    
        if ($(this).is(".up") && row.prevAll().length > 1) {
            row.insertBefore(row.prev());
        }
        else if ($(this).is(".down") && row.nextAll().length > 0) {
            row.insertAfter(row.next());
        }
        else {
            return false;
        }
    });
    

    JS Fiddle demo.

    Incidentally, and as an aside, your script may have been generating errors from the comment (though I do suspect the were added for our benefit, rather than in your actual JavaScript) syntax:

    <!-- comment text -->
    

    is html syntax, and within the confines of the <script></script> tags is invalid, to comment JavaScript use either:

    // single-line comment
    // which needs to have the double-slash
    // preface each comment-line...
    

    or:

    /* This is a multi-line
       comment, and this text
       will happily remain commented-out
       until you get to
       one of these comment-close things, just to the right -> */
    

    References:

    • .prevAll().
    • .nextAll().
    • .length.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that has a fixed layout of rows. Each row has
I have a join which deletes rows that match another table but the joining
I have a UITableViewController and want to move down a cell in my table
On my pages I have a table with rows that typically look like this:
I have two functions to add and remove table rows that contain a form
I have a MySQL table with about 5,000,000 rows that are being constantly updated
I have a table that looks like that: The rows are sorted by CLNDR_DATE
I have a large table (~1M rows now, soon ~10M) that has two ranked
I have a table that contains maybe 10k to 100k rows and I need
I have a table body that grabs it's rows (and form fields) via AJAX

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.