Javascript Controls - Resizeable Textbox, Part Tres
| It actually isn't that much of a change from how adding the resizeable textbox worked before, and what did change is pretty cool, so we are going to go into some detail on how it works. We are also going to update our previous controls with this new method as well (the spin control and the trackbar), but for those we are just going to add an addendum onto those posts. And so below we have our classic example of the resizeable textbox - hopefully, it looks exactly the same as it did before. The only difference now is that the code to add it just got a bunch simpler: |
And here is the entire block of code required to do exactly that:
<div style="position:relative;width:530px;height:250px;
border:1px solid black;">
<script type="text/javascript">
var rt = new ResizeableTextbox();
rt.SetText("I Am Some Text");
rt.GetContainer().style.left = '10px';
rt.GetContainer().style.top = '10px';
rt.SetMaxWidth(510);
rt.SetMaxHeight(230);
</script>
</div>
border:1px solid black;">
<script type="text/javascript">
var rt = new ResizeableTextbox();
rt.SetText("I Am Some Text");
rt.GetContainer().style.left = '10px';
rt.GetContainer().style.top = '10px';
rt.SetMaxWidth(510);
rt.SetMaxHeight(230);
</script>
</div>
The key here is the ability to add in place - i.e., wherever you put the javascript block that creates the textbox, that is where the textbox appears. Here, the javascript block is inside that container div, so the resizeable textbox is added inside of that div. If you have ever used Google Adsense, you might notice the similarity - where ever you put a Google Adsense script block, that is where the ad appears.
So how do we do this? It actually is pretty simple, although it is kind of ugly. You will see what I mean as you read the code:
var id = "sotc_rt_" + new Date().getTime() + Math.round(Math.random()*2147483647);
while(document.getElementById(id) != null)
id += Math.round(Math.random()*2147483647);
document.write('<span id="'+ id + '"></span>');
var element = document.getElementById(id);
element.parentNode.replaceChild(_container, element);
while(document.getElementById(id) != null)
id += Math.round(Math.random()*2147483647);
document.write('<span id="'+ id + '"></span>');
var element = document.getElementById(id);
element.parentNode.replaceChild(_container, element);
This is a chunk of code from the Resizeable Textbox object, where the actual insertion of the object into the document is taking place. Essentially, what we need to do is find where in the document we currently are, so that we can add the textbox here. We do this by writing a temporary element to the document using
document.write - and the document.write call will write the element immediately after the end of the current element, i.e., the script element creating the textbox. Then we retrieve the element using getElementById, and use a replace child to replace the temporary element with the actual textbox container element.There are a couple oddities there you might be wondering about. First, what is all the fuss in the code around creating an id for the temporary element? Well, we need all that to make sure that the id is absolutely unique in this document, because we need to retrieve the element object again using
getElementById. So we go to great lengths to create a random id, and if for some crazy odd reason that id is actually in use, we add even more randomness on top of it until we get a unique id. The other question you might have is why we don't just write the entire component out to the page using document.write. Really, it was just a lot easier to do given the current structure of the resizeable textbox code, since it was written using a lot of document.createElement and no strings of html code.We made two other small changes to the resizeable textbox code, and they also have to do with ease of use and ease of adding one to a page. We made two optional constructor arguments, the first one being the id you want the textarea to have, and the second being the element you want the resizeable textbox added to (in case you don't want it added directly where the javascript block is). So, for instance, you could create a resizeable textbox like so:
var rt = new ResizeableTextbox('MyBestID');
That would create a resizeble textbox in place, with the ID and name set to "MyBestID".
Or you could create one like this:
var rt = new ResizeableTextbox('MyBestID', 'MyFavElement');
And this would create a resizeable textbox, add it to the element with the id 'MyFavElement', and set the ID and name to 'MyBestID'.
And that is it for this update to the resizeable textbox control! Hopefully, you are having fun with this control, and if you have any ideas for making it even easier to use, just let me know. As always, you can download the full source, and if you have any questions please leave them in the comments.
Posted in Javascript, All Tutorials by The Tallest |

May 23rd, 2008 at 9:53 pm
css textboxt ınput (textfield) style - examples - -
http://www.css-lessons.ucoz.com/textbox-css-examples.htm
June 1st, 2008 at 4:19 pm
First of all I must congratulate you guys with this excellent piece of software, works like a charm and thankfully without a huge library like prototype and the likes.
There are however a few problems, currently the script pollutes the namespace, if-statements with a single line statement are not enclosed by curly brackets and the injection code that creates the textarea, well… you can do better than that
The namespace pollution can be solved pretty easy, just pop everything inside a singleton and there you have it.
The curly brackets are solved also pretty easy, now your script can be compressed without errors (there are some semi colons missing though).
And the injection code, I think that should be fixed by calling the ‘ResizeableTextbox’ function with a parameter that points to the div the textarea should be added to. Or perhaps an existing textarea that should get the resize ancors… I’m not sure about the best approach. :s
Keep up the good work guys and remember, 2 spaces does not a tab make.
June 7th, 2008 at 5:43 am
HI i need your help i really want to create my own website/web page but i dont know how to go about doing it so can you please help me out
July 8th, 2008 at 9:50 am
I like this very much, but I’m having a little difficulty.
I’ve tried a number of resizble textarea scripts, and they all result in me having the same problem.
I’m using the textarea in a form, but when I submit the form, the textarea isn’t included in the POST Data.
Any ideas?
August 29th, 2008 at 7:31 am
Great peace of code! … but… same problem here - the textarea isn’t included in the POST Data.
Please give an advice!