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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:17:27+00:00 2026-06-15T22:17:27+00:00

I have a basic HTML table that gets loaded. The structure differs based on

  • 0

I have a basic HTML table that gets loaded. The structure differs based on and uploaded CSV file’s content. The table columns need to be drag-able (for which I use a plugin called Dragtable). I then have methods to add rows and columns. The add column method looks like this:

function addCol(tblId) {
 var tblHeadObj = document.getElementById(tblId).tHead;
 for (var h=0; h < tblHeadObj.rows.length; h++) {
    var newTH = document.createElement('th');
    tblHeadObj.rows[h].appendChild(newTH);
    colCount = (tblHeadObj.rows[h].cells.length);
    newTH.innerHTML = 'Col ' + colCount;
 }

 var tblBodyObj = document.getElementById(tblId).tBodies[0];
 for (var i=0; i<tblBodyObj.rows.length; i++) {
    var newCell = tblBodyObj.rows[i].insertCell(-1);
    newCell.innerHTML = "*null*";
 }
}

The problem is that when I add a new column, I can no longer drag that column to place it in a different spot. I also have an onclick handler for table headings which allow the user to delete a column when he clicks on the th element. This also does not work on newly added columns. Basically, all event listeners are not added on new columns.

I have tried hiding and showing the table and the page, which does not work. I think I need a way for the table to be refreshed, but cannot find something that works.

Something like this:

var table = document.getElementById("grid");
table.refresh ();

Is there a way to accomplish this? Or is there something wrong with the way I add columns which could be the reason why the event listeners are not attached?

EDIT

The delete column method is below: (Edited with proper event delegation) This fixed the problem for the delete column method. I am still unable to drag and sort new columns though

$("table").delegate("th:not(:first)", "click", function() {
   var index = this.cellIndex;
   $(this).closest('table').find('tr').each(function() {
     this.removeChild(this.cells[ index ]);
   });  
}

Regarding the Dragtable column sorting, I just add the draggable class to my table. I’m not sure how the event listener looks:

<table id = "grid" class="draggable"></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-15T22:17:29+00:00Added an answer on June 15, 2026 at 10:17 pm

    You should use event delegation for this. What you should do is add an event listener to the table itself, which checks the .target property of the event object to see if the event is targeted at a heading and takes the appropriate action.

    Aside from the event delegation approach, another easy fix you could make is to simply attach the handler when you add a new row:

    newTH.addEventListener("click", function(){
         //this should be your event listener
    })
    

    As j08691 has pointed out, you may have tagged jQuery mistakenly, so I am not adding any jQuery code for now

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

Sidebar

Related Questions

I have a .jsp page that displays a payment table with 4 columns. Based
I have a basic string with holds a html table. The table looks like
This is probably a basic html/css question... I have a simple one-button form that
I have a page with some very basic examples that use HTML/CSS/JavaScript. I'd like
I have a js.erb with this basic content: $(#currencies).html(<%= options_for_select([['Dollar', '$']])%>); And the html
We have a simple ColdFusion page that is outputting basic HTML to the browser.
I have a basic table with a div contained in it that I bring
I have set up a basic html table with a reader in each cell
Hey all, I have a basic HTML contact form that goes to a .php
MVC 3, VB.NET. I have a form in my app that gets basic information

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.