• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

Change table column width

hi all

i need to change the column width of a table dynamically...

eg: i have created a table with some rows and columns... and then i need to change the column width by dragging the column border.....

Can anyone help me in this .. :-)

  • 2
1 Solution
Hi sumachaa, for 50 points on such a complicated question, I'll point you towards a similar question:


And for end result:


mplungjan seems to be working on something similar. Not sure how you'll integrate it into what you are doing, but all of the workings are there.

I tried something similier to this before, and I found it very hard to actualy select the border, what I had to do was set up a table with a very skinny column, not much wider than a normal column(just take off all padding and put a space in it with a very small font 1px)  then you can just use the on mouse down function.

at this point just grab the window.clientY and then on mousemove just set the width on the left hand cell to match the window.clientY - cells left offset.  this should do it, just make sure you don't have a width in the right hand cell.

if you raise the point up I could try to write some of the actual code for you.

Hi sumachaa, I am feeling very generous today. If the below is to your liking, and you wish to accept this solution as the answer, I'd greatly appreciate a hike in points to accomodate the level of difficulty this answer was to complete. It is a modification of the questions I gave a link to above.

Any modification of the script to suit your needs will have to be done by you or asked in another question.

Adjustable TD Width:

<script type="text/javascript">
// Store a value from 0 to 100 related to the slider's position

// Slider's head left and top

// Drag action is on going is set to true

// Mouse cursor position relatively to the slider's head

// Slider's container left and top boundaries

// Get and set positions
function getAbsLeft(o) {
     oLeft = o.offsetLeft
     while(o.offsetParent!=null) {
          oParent = o.offsetParent
          oLeft += oParent.offsetLeft+10;
          oLeft -= oLeft
          o = oParent
     return oLeft

function getAbsTop(o) {
     oTop = o.offsetTop
     while(o.offsetParent!=null) {
          oParent = o.offsetParent
          oTop += oParent.offsetTop
          o = oParent
     return oTop

function setLeft(o,oLeft) {
     o.style.left = oLeft + "px"

function setTop(o,oTop) {
     o.style.top = oTop + "px"

function setPosition(o,oLeft,oTop) {

// Slider's head mouse down handler
function sldMouseDown(e)
     // Get event object for IE
     if (!e) {e = window.event}
     // Drag action begins
     dragElement = "sldHead"
     // Get slider's head position

  savePos["sldHead"] = sldLeft;

     // Get mouse cursor position relatively to the slider's head

     // Get slider's container boundaries


// Generic mouse up handler
function sldMouseUp(e)
     // Drag action stops
     document.body.style.cursor = "default";
     o3 = document.getElementById('dragTD').offsetWidth-4;
     o.style.left = o3;
savePos = new Array();

// Generic mouse move handler
function sldMouseMove(e)
     // Get event object for IE
     if (!e) {e = window.event}
     // If drag action is on going...
     if (doDrag)
          o3 = document.getElementById('dragTD').offsetWidth-4;
          o.style.left = o3;

          // Get slider's head new position
          if(e.clientX < o.offsetLeft+10){
              newPos = 10;
              newPos = e.clientX-sldMouseLeft
          document.getElementById('dragTD').style.width = newPos-4;
          // Check slider's container boundaries
          if(newPos>o3) {maxLeft=o3;newPos=maxLeft}



// Set generic handlers
document.onmousemove = sldMouseMove;
document.onmouseup= sldMouseUp
function setDiv(){
    document.getElementById('sldHead').style.left = document.getElementById('dragTD').offsetWidth-3;
<body onload="setDiv();">
<table align=center id="sldContainer" border="1" cellpadding="0" cellspacing="0" style="position:relative;top:0px;left:0px;width:600px;height:300px; background:#FFFFFF;" onselectstart="return false;">
     <td id="dragTD" style="border-collapse:collapse;border-right:2px ridge;width:200px;padding: 0px 0px 0px 4px;font-weight:bold;overflow:hidden" onselectstart="return false;">
TD1 Content Goes Here<br>
(Mouse Over the TD Border to Resize TD1)
<div id="sldHead"
style="position:absolute;top:0px;left:0px;width:6px;height:300px;cursor:e-resize;background:transparent;overflow:hidden;" onmousedown="sldMouseDown(event)" onmousemove="sldMouseMove()">
     <td style="padding: 0px 0px 0px 4px;font-weight:bold;background:#000000;color:#FFFFFF;"> TD2 Content Goes Here

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now