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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:19:01+00:00 2026-05-27T19:19:01+00:00

I have a html table that looks something like this <table id=eventTable> <thead> <tr><th>#</th><th>Options</th></tr>

  • 0

I have a html table that looks something like this

<table id="eventTable">
  <thead>
    <tr><th>#</th><th>Options</th></tr>
  </thead>
  <tbody>
    <tr id="6">
      <td>6</td><td><a href="www.site.com" class="moveUp">Up</a><a href="www.site.com" class="moveDown">Down</a></td>
    </tr>
    <tr id="5">
      <td>5</td><td><a href="www.site.com" class="moveUp">Up</a><a href="www.site.com" class="moveDown">Down</a></td>
    </tr>
    <tr id="2">
      <td>2</td><td><a href="www.site.com" class="moveUp">Up</a><a href="www.site.com" class="moveDown">Down</a></td>
    </tr>
    <tr id="4">
      <td>4</td><td><a href="www.site.com" class="moveUp">Up</a><a href="www.site.com" class="moveDown">Down</a></td>
    </tr>
  <
</table>

<p><a href="www.site.nl/process.php?type=order" id="saveOrder">Save order</a>

I use the following functions to change the row order


  $(".moveUp").click(function(e){
    var event = e || window.event;
    event.preventDefault();
    var row = $(this).closest('tr');
    row.prev().insertAfter(row);
  });

$(".moveDown").click(function(e){ var event = e || window.event; event.preventDefault(); var row = $(this).closest('tr'); row.insertAfter(row.next()); });

When the order is complete i have to save it (ofcourse). I have a textlink that passes the value via php to update the values in a database. My problem is how i can find the new position of the elements in the table. So far i have this function


  $("#saveOrder").click(function(e){
    var event = e || window.event;
    event.preventDefault();
    var t = "";
    $("#eventTable tr").each(function(){
      t += this.attr("id") || "";
      alert($(this).rowIndex);
    });
    window.location.href = $(this).attr("href")+"&sort="+t;
  });

This only passes al id attribute values from the table rows but i, somehow, need to pass the new tablerow-order as well. Anyone any idea how t achieve this? this.rowIndex doesn't seem to work

EDIT:

I tried Jasper's solution, but it's not fully (but a nice half ;)) what im looking for. See my example above, the id attributes represent the unique id of the database record. When i change the order of the table rows, i need to match the database id with the new position.

So at the start, my output coding would be

61|52|23|44
id 6 == row 1
id 5 == row 2
id 2 == row 3
id 4 == row 4

Suppose i switch the position of id 5 and 2, i want my output to be like this:


61|22|53|44
id 6 == row 1
id 2 == row 2
id 5 == row 3
id 4 == row 4

Hope this made it clear 🙂

Thanks for the help!

  • 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-27T19:19:02+00:00Added an answer on May 27, 2026 at 7:19 pm
    $('#saveOrder').click(function (e) {
        e.preventDefault();
        var $TRs = $('#eventTable').find('tr[id]'),//this only selects TR elements that have an ID attribute
            out  = [];
        for (var i = 0, len = $TRs.length; i < len; i++) {
            out.push($TRs.eq(i).attr('id'));
        }
        //now the ids are in order from top to bottom and stored in an array (`out`)
        window.location.href = $(this).attr("href") + "&sort=" + out.join('|');
    });
    

    Here is a demo: http://jsfiddle.net/mAUtk/1

    Also on a side-note, your ids are not valid; they should not begin with a number. Here is a great post about how to create a valid id attribute in HTML: What are valid values for the id attribute in HTML?

    You can just change the id attribute to data-id which is valid HTML5 code.

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

Sidebar

Related Questions

I have an HTML table that looks like this: ------------------------------------------------- |Column 1 |Column 2
I have a table that looks like this: (so you can picture it visually)
I have a model that looks something like this: public class SampleModel { public
I have a table that looks like the following. <tr> <td>Link Name</td> <td><a href=#
I have an HTML table that I'd like to be able to export to
This one is weird, I have a page that consists of a html table
I have a view model that has a property that looks like this Property
I have a Javascript object that (very simplified) looks like this if('undefined' !== typeof(listCtrls)){
I have to build an HTML table that shows data for users versus pages
I have an html table containing values that are being generated from javascript. How

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.