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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:31:17+00:00 2026-05-28T13:31:17+00:00

I am writing a jquery plugin that gets a table and allow to change

  • 0

I am writing a jquery plugin that gets a table and allow to change the columns order.
The code for putting the column that in position oldIndex in position newIndex is:

table.find('> thead > tr, > tbody > tr').each(function() {
    var row = $(this);
    var children = row.children();
    var source = $(children[oldIndex ]);
    var destination = $(children[newIndex ]);

    if (oldIndex != newIndex ) {
        destination
            .replaceWith(source)
            .appendTo(row);
    }
});

The problem is that each td has events that came outside this code. When using replaceWith, it removes those events.

Any idea hoe can I replace position of DOM element and preserve its events?

  • 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-28T13:31:18+00:00Added an answer on May 28, 2026 at 1:31 pm

    Make sure that the bound functions are attached to the to-be-moved element.

    Instead of using replaceWith, I suggest to use logic to swap the columns. .eq is used to select the index of a specific column, .after() and .before() are used to swap the columns:

    Demo: http://jsfiddle.net/SfwXg/

    // Indexes are zero-based
    var oldIndex = 1;  // = Second column
    var newIndex = 2;  // = Third column
    var table = $('table');
    
    if (oldIndex != newIndex) {
        if (oldIndex > newIndex) {
            // Let newIndex always be higher than oldIndex
            var tmp = oldIndex;
            oldIndex = newIndex;
            newIndex = oldIndex;
        }
        table.find('> thead > tr, > tbody > tr').each(function() {
    //or:table.children('thead,tbody').children().each(function() {
            var row = $(this);
            var children = row.children();
            
            var right = children.eq(newIndex);
            var left = children.eq(oldIndex);
            
            children.eq(newIndex).after(left);
            if (newIndex != oldIndex+1) {
               // If they're next to each other, don't swap the columns
               children.eq(oldIndex+1).before(right);
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm playing around with writing a jQuery plugin that uses an attribute to define
When writing a new jQuery plugin is there a straightforward way of checking that
I am writing a simple GreaseMonkey script that has a jQuery plugin called hoverIntent
I'm writing a semi-generic form plugin using jQuery in order to speed up the
I'm writing a jQuery plugin that has a couple of callbacks, and am at
I'm writing a jQuery UI plugin. Inside that plugin, when an action occurs, I'm
I'm writing a jQuery plugin for a toolbar that has a few buttons in
Me and a friend are writing a plugin for jQuery that makes it easy
I am writing a jQuery plugin that references another plugin, I dynamically add the
I'm writing a jQuery plugin, and I was wondering how to make sure that

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.