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
September 5th, 2008 at 6:53 am
how i fetch data from database from a particular table
December 4th, 2008 at 8:07 pm
How do i get content from the text box? i was looking at the code and there is no function available to fetch the current content in the text-box, i can add content to the text box, but i would like to retrieve the content from a text-box – any ideas?
December 11th, 2008 at 12:50 am
Re: Veeru’s question.
This was bugging me as well. It turns out you can just call the update() method on the FacebookList or TextboxList object. For example, call tlist2.update() before submitting the form and you should be fine. Just adding that call to the form’s onsubmit worked for me:
August 19th, 2009 at 6:44 am
[...] 7. Facebook-like Inputs With Prototype [...]
August 27th, 2009 at 7:24 pm
[...] Demo Download 7. Facebook Prototype输入框 [...]
September 7th, 2009 at 6:39 am
If I paste a string with commas, the entire string will be treated as a keyword which imho is not as it should be. Please consider this addNewTag():
addNewTag: function() {
var tlist = P5.tb.inputName;
var _input = document.getElementsByClassName(‘maininput’);
var tarr = $F(_input[0]).split(‘,’);
for (var j=0; j 0)
{
tlist.add(text);
}
}
_input = document.getElementsByClassName(‘maininput’);
for(var i = 0; i<_input.length; i++)
{
_input[i].value = “”;
}
},
September 14th, 2009 at 6:58 am
[...] asli berbasis MooTools, tapi ada juga yang menggunakan Prototype buatan InteRiders. Cek demonya di Devthought untuk TextboxList MooTools dan InteRiders untuk TextboxList [...]
October 8th, 2009 at 7:45 am
Nice plugin! But it is possible to transform it for jquery ???
November 1st, 2009 at 3:48 am
Nice one…
But did you know it doesnt work with IE8? Styling is messy…
March 2nd, 2010 at 7:40 pm
[...] Facebook Prototype输入框 Facebook的输入框设计非常不错,这个教程教你如何实现类似效果。 Demo | [...]