TextboxList: Fancy Facebook-Like dynamic inputs! Now, in Prototype

Hey everybody, after coming across Guillermo Rauch’s script, I knew i had to port it into my favorite library, Prototype. In this page you will find the Guillermo's script translated into Prototype. I kept the original formatting and will post it just like he did for the ease of understanding for people who used the original first. Later on this week, I will release an improved version of the script. This post is a conversion of the original deal. The improved script will include more features and implement several improvements. Let me include the original post by Guillermo that describes the script. The post is slightly modified to suite my blog's format. Have fun, and please! report any glitches or feature requests so I will implement those in the improved version, due later on this week.
Original post:
Check out a demo of TextboxList before reading!
While working on my big and exciting new project, I decided to include an input that resembles the famous Apple Mail to: textfield. I’d seen it in Facebook before, which has a really decent implementation of this concept (it work well, but it doesn’t respect any modern programming principles; basically, it’s a big tag soup with lots of inline Javascript)
I created my own, MooTools 1.2 [Now: Prototype] compatible, in just 5kb. It’s not only small, but also really frexible! Here are some notes of the creation process and how to implement it in your own projects.
Anatomy of the control
As usual, I try to come up with a semantic, unobtrusive approach. I start with the CSS and the markup that will be my end result.
I want to go from something like this:
to something like this (only one possible scenario, naturally)
-
<ul class="holder">
-
</ul>
-
</li>
Basically, it’s a group of pieces (that I’ll call bits), that can be either a box or an input (small, except for the main one). The user is able to move around between the bits by using his keyboard or his mouse.
Javascript
As far as the javascript goes, I try to first think about reusable code (usually classes) that I may use. I thus first coded the class that adds resizing capabilities to the small fields as the user types, and a small utility method to find the caret position.
The only challenges I found was handling the complex events while keeping everything crossbrowser. Again, none of this would have been possible if it wasn’t for MooTools (1.2) [Now: Prototype].
Using it
All you have to do is:
-
new TextboxList('input-demo');
Where input-demo is the id of the desired input to replace
The constructor can take these options:
* onInputFocus (event, fired when an input gets focus)
* onInputBlur (event, fired when an input loses focus)
* onBoxFocus (event, fired when a box gets focus)
* onBoxBlur (event, fired when a box loses focus)
* onBoxDispose (event, fired when a box is removed)
* resizable (option, hash, passed to ResizableTextbox constructor)
* className (option, string, prefix of the classnames of the generated objects)
* extrainputs (option, boolean, adds small inputs between boxes if true)
* startinput (option, boolean, adds a small input before the first box if true)
* hideempty (option, boolean, hides the small inputs by default)
Download
Click here to download the cool script ![]()
Posted in Prototype/JavaScript/Ajax

February 13th, 2008 at 11:22 am
Thanks This looks really nice.
February 15th, 2008 at 9:55 am
This is pure candy.
Thanks!
February 15th, 2008 at 12:27 pm
[...] Ubergizmo - Español wrote an interesting post today on TextboxList: Fancy Facebook-Like dynamic inputs! Now, in Prototype |…Here’s a quick excerpt [...]
February 15th, 2008 at 12:48 pm
[...] BigBrotherAccess.com wrote an interesting post today on TextboxList: Fancy Facebook-Like dynamic inputs! Now, in Prototype |…Here’s a quick excerpt [...]
February 17th, 2008 at 6:22 am
[...] InteRiders there is also a version in prototype available. Mental notice: If I have time, I should implement a [...]
February 22nd, 2008 at 11:48 pm
[...] more and download the source from the below link. http://www.interiders.com/2008/02/11/textboxlist-fancy-facebook-like-dynamic-inputs-now-in-prototype... Posted in AJAX Showcase, AutoComplete [...]
March 8th, 2008 at 8:30 pm
I like this control a lot but need two minor changes:
1. Is there a way to reject user input that doesn’t come from the selection. As it is it currently lets you type in anything irregardless if it’s in the list
2. Is there a way to bind the value to a key (non-visible) instead of the string being displayed?
Thanks,
GP