So something that seems to be popping up a bunch on various sites lately is the resizeable textbox. Some are implemented with a resize handle on the corner, others let you drag any of the sides. I thought they were kind of cool, so I sat down to see how hard it would be to write. And, surprisingly enough, it is actually pretty easy. So today I'm going to walk through some resizeable textbox code. It is not very pretty, either code-wise or look-wise, but it does get the job done.
So what we are going to create today is a textbox with resize handles on the bottom, right, and bottom-right corner. The resize handles do what you might expect - grabbing the right edge will let you resize the width, grabbing the bottom will let you resize the height, and grabbing the corner will let you resize either. The bulk of the work is actually done by code in a previous tutorial, about how to do dragging in javascript. So you will probably want to read that before you continue.
So here is the example. Ugly, but pretty cool:
Because we have the drag object from the draggable elements tutorial, the code is actually pretty simple. So lets take a look at the html first:
<textarea id="textBox" style="width:90px;height:90px;left:0px;top:0px;
position:absolute;">
</textarea>
<div id="handleRight" style="width:5px;height:95px;background-color:Red;
position:absolute;left:95px;top:0px;cursor:e-resize;"></div>
<div id="handleCorner" style="width:5px;left:95px;top:95px;height:5px;
background-color:LightGreen;position:absolute;cursor:se-resize;"></div>
<div id="handleBottom" style="width:95px;height:5px;background-color:Blue;
position:absolute;left:0px;top:95px;cursor:s-resize;"></div>
</div>
Not really much there. First there is a surrounding div, so that we can position the other elements against it. The textarea is in the upper left, and is initially set to take up 90px by 90px. On the right side, we have a 5 pixel wide red div, 95 pixels from the left. This div is the right resize handle. The reason we leave a 5 pixel gap between the texarea and the handle is that for some reason a 90px textarea takes a little bit more than 90 pixels when the scrollbar appears. So we leave those 5 pixels free to give the textarea some breathing room. Next, we have our green corner resize handle, a 5x5 pixel div at position 955x95. Finally we have the blue bottom resize handle, 5 pixels tall and 95 pixels from the top. Giving the divs the appropriate resize cursors is really easy - we just set the cursor style attribute the way we want.
Ok, on to the javascript initialization code:
var handleRight = document.getElementById("handleRight");
var handleCorner = document.getElementById("handleCorner");
var handleBottom = document.getElementById("handleBottom");
var textDiv = document.getElementById("textDiv");
new dragObject(handleRight, null, new Position(15, 0), new Position(620, 0),
null, RightMove, null, false);
new dragObject(handleBottom, null, new Position(0, 15), new Position(0, 400),
null, BottomMove, null, false);
new dragObject(handleCorner, null, new Position(15, 15), new Position(620, 400),
null, CornerMove, null, false);
We first grab the various elements and store them in variables, mostly because we are going to be accesing them so much. No need to make the browser waste time looking them up again and again. Next, we make the three dragObject instances, one for each of the handles. We hand each a minimum and maximum bound. By handing 0 in for both the y min and y max on the right handle, we ensure that we can only move left and right, and not up and down. This is also why we pass in 0 as the x min and max on the bottom handle - we only want to be able to move that handle up and down, not left and right. The 15 and the 500 that we pass in as the rest of those bounds allow us to set a minimum and maximum size on the textbox. By passing in 15 as the x min and 500 as the x max for both the right and corner handle, we make it so that the textbox can't be made bigger than 495px wide and can't be made smaller than 10px wide (the 5px difference is because of that breathing room we gave the textbox earlier). The same thing applies to the height, because we pass in 15 as the y min and 500 as the y max on both the bottom handle and the corner handle.
The other important argument here is the callback functions RightMove, BottomMove, and CornerMove. Everytime one of the handles is moved, the appropriate callback will be called (because of code deep inside the dragObject). So lets take a look at what these functions do:
{
DoHeight(newPos.Y, element);
}
function RightMove(newPos, element)
{
DoWidth(newPos.X, element);
}
function CornerMove(newPos, element)
{
DoHeight(newPos.Y, element);
DoWidth(newPos.X, element);
}
Wow, there is virtually nothing there. But before we move to the DoHeight and DoWidth functions, lets review what these arguments are. The newPos is a position object holding the (top,left) position of the element that is being moved. The element variable is the exactly what it sounds like - the element being moved.
Ok, now on to where the real work happens:
{
textDiv.style.height = (y + 5) + 'px';
if(element != handleCorner)
handleCorner.style.top = y + 'px';
handleRight.style.height = y + 'px';
if(element != handleBottom)
handleBottom.style.top = y + 'px';
textBox.style.height = (y - 5) + 'px';
}
function DoWidth(x, element)
{
textDiv.style.width = (x + 5) + 'px';
if(element != handleCorner)
handleCorner.style.left = x + 'px';
if(element != handleRight)
handleRight.style.left = x + 'px';
handleBottom.style.width = x + 'px';
textBox.style.width = (x - 5) + 'px';
}
These two functions do exactly what they sound like they do - they calculate the new width and height of the textbox (and move and resize the handle elements as appropriate). In both, we first adjust the size of the big container div, taking into account the width (or height) of the handles. Then, if the corner handle is not the one doing the resizing, we move it into place (if it is the one doing the resizing, it is already in place). For the right handle, if it is a height resize, we update the height of the handle. If it is a width resize, we check to make sure it is not being done by the right handle, and if it is not, we update the x position of the handle. For the bottom handle, we doing essentially the same thing, except we update the width during the width resize, and the y position during the height resize. Finally, we update the size of the actual textbox, taking into account the breathing room.
And that is it! With that code, and the dragging code, you can get resizeable textboxes. It is true that these are pretty ugly and limited, so don't be surprised if at some point in the future we have a resizable textboxes 2.0 tutorial. But until then, feel free to leave questions or comments.
10/07/2007 - 11:01
Thanks for idea, but script length is too large to understand how it works fast enough. I'll have to make my own one, sorry.
10/07/2007 - 18:16
Yeah, the code for the drag object is pretty complicated. Sadly, though, there really isn't any way around that when making generic dragging work in javascript.
Glad you liked the idea, and I hope you have fun writing your own!
10/11/2007 - 07:51
The script works fine, very nice really. Thanks
11/11/2007 - 19:21
Ya that is good idea for Ajax and scripts are work very wall
11/14/2007 - 14:03
yeah its good!
12/04/2007 - 17:29
very good!
This is a fantastic way to use more or less space!
01/03/2008 - 00:34
wonderful nice job
01/07/2008 - 15:43
Nice and beautiful. But I doesn;t like the code for draging part. Too long and complicated. I spot a code somewhere, which can be done within less then 20 lines for drag and drop.
01/11/2008 - 01:57
Nice and beautiful.
01/11/2008 - 15:11
Does it work with Firefox? It doesn't work in my browser...:=(
01/11/2008 - 15:42
Yup, it works in Firefox. It should also work in Opera, Safari, and IE 6 & 7. But if this version isn't working for you, you can check out the slightly nicer version we made a couple weeks after this one in this post.
02/04/2008 - 05:10
I was asked to implement resizing textbox in one of our project. I tried implementing that with the help of this article (It is really a nice one). But in my case, I wanted to implement this functionality for morethan one textbox object within the same form. I coudn't achieve that. Will you please give me the solution or suggestion how to go about this.
Thanks & Regards,
DhanaSekaran. R
03/07/2008 - 10:57
muchas gracias, exelente post
03/26/2008 - 06:05
Hi.. i want a different types of text boxes, like changing the border size,text box color,text box border color and so on.. can you help me upon this.
04/18/2008 - 07:41
It's great. I'll be using it. Thanks a lot.
06/13/2008 - 01:36
Bug, in SOTC-ResizeableTexbox-v1.js
line 191:
elementStartPos = elementStartPos.Check();
should be
elementStartPos.Check();
This method alters the members of elementStartPos. It does not return a value. Hence, the value is nullified.
06/13/2008 - 06:07
Actually, ddoctor, that is not a bug. You must have misread the
Checkfunction, because it does in fact return return a value (see line 76).07/13/2008 - 23:07
Thanks for the very helpful information.
Best Regards
Offshore Software Development
http://www.softwebsolutions.com
07/20/2008 - 21:58
Um...I don't know why you made the drag and drop so complex. It takes about 4-5 functions and a bit more then 30 lines of code to get a generic drag and drop working that is cross browser compatible. This is the first time I'm visiting your site, and its not a coding style that I'm used too. There are many ways to get something done, the more lines of code does not necessarily mean the smartest way to do it.
07/21/2008 - 06:29
See my comment here for a response.
03/09/2009 - 05:38
using div how we are make the textboxes
09/27/2008 - 11:33
Hi,
I am actually a C# programmer and yeah,
i understand this code perfectly.
very similar to .NET style code.
thanks :)
10/03/2008 - 03:11
Good style code
05/27/2008 - 04:57
Superb code man.......
Thanks
I wander >>>>
11/20/2008 - 17:29
thanks you şükran :)
11/20/2008 - 17:39
hello
who can help me with auto run ?
thx
11/28/2008 - 16:27
thanks you wery much admıns WELCOME...
12/05/2008 - 17:53
thnaks yours
01/02/2009 - 17:43
thank you
02/18/2009 - 14:00
Nice tutorial, but I acually prefer the resizable textbox use for the "Add Comment" section of your blog.
How can one implement this (maybe using a framework with a plugin like JQuery)?
02/18/2009 - 15:02
That box actually just comes with Drupal. Drupal is entirely open source, so you should be able to find it. I would find it for you, but their site is down today from 6:00pm to 10:00pm.
02/18/2009 - 22:36
I looked it up. They use jQuery. Here's a link to the textarea.js file, which contains the resize code.
03/01/2009 - 18:11
Thanks! Gracias! Merci! Danke!
04/11/2009 - 04:54
Very best it i am findind since last 5 days
Thanks
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.