Change table column width

Posted on 2004-07-30
Last Modified: 2010-05-18
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 .. :-)

Question by:sumachaa
  • 2
LVL 14

Expert Comment

ID: 11676838
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.


Expert Comment

ID: 11677439
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.

LVL 14

Accepted Solution

ziffgone earned 50 total points
ID: 11689764
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) { = oLeft + "px"

function setTop(o,oTop) { = 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""e-resize";
     // 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
     doDrag=false = "default";
     o3 = document.getElementById('dragTD').offsetWidth-4; = 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;
 = 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


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
need help with share buttons 11 69
Sending ASP to server side 8 31
Html using "Or" in condition 3 26
Two decimal 5 26
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question