BIGREDSWITCH Push it. You know you want to.

jQuery Dynamic TextboxList with Autocomplete

Hmmm, had a hard time naming this since I couldn’t think of a title that was succinct and more descriptive than the generic one above.  Any suggestions?  Update: it’s official! In honor of Transformers everywhere, it’s now called autobox.

Anyway, let’s just show the plugin:


  1. Type an email or list name
    • fred@foo.bar
    • barney@foo.bar

Now the story:

Guillermo Rauch has a very nice mootools script that does a Facebook-like or AppleMail-like input control: thebasic one here and here with autocompletion.  The first thing I noticed is that Guillermo is only 17 years old! If you look at his original code, it’s clean and well written even if he was an industry veteran - amazing!

This plugin is what we’re looking for in our email composer.  Also, I was looking to actually cutting my teeth a little bit in javascript and jQuery (I’m currently reading chapter 3 of the jQ in Action book).  This points at porting this thing so here it is:

Update (12/3): Use new autobox2 plugin!
Update (10/23): Connect input behavior to submit values; updated example
jq-autobox-plugin-052.zip
Update (10/21): Rename to autobox
jq-autobox-plugin-051.zip
jq.textboxlist.plugin-050.zip

Notes on the port:

We would love to get feedback on the control!  The release is 0.5.0 (alpha); I’m sure there are problems with it and bug reports would be appreciated as well.  I will release at least a few more versions as they get cooked and I get more familiar with jQuery and Javascript.

Quick instructions to create the demo above:

  1. In your HTML, include jQuery, the plugin (jquery.textboxlist.js), and the CSS (jquery.textboxlist.css).
  2. Put close.gif in the same directory or else change the css to point to where you put it.
  3. The CSS assumes a form with ordered list and list element container.  I.e. the CSS assumes the form as in the example.
  4. Below the main input element, in the same list element, add a div element with the ID from above appended by ‘-auto’, this is the autofeed.  See the example.
  5. In a script tag, get the ID and call ‘.textboxlist()’ on it to make it a textboxlist.
  6. Optionally pre-populate any autofeed elements.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>jQuery TextboxList Demo</title>
<link rel="stylesheet" href="jquery.textboxlist.css" type="text/css" media="screen" title="Test Stylesheet" charset="utf-8" />
<script src="../jqueryjs/jquery/dist/jquery.js" type="text/javascript" charset="utf-8"></script>
<script src="jquery.textboxlist.js" type="text/javascript" charset="utf-8"></script>
<style>img{ height: 100px; float: left; }</style>
</head>
<body>
  <form action="test_submit" method="get" accept-charset="utf-8">
  <ol><li>
      <label>Test input</label>
      <input type="text" value="" id="test-1"/>
      <div id="test-1-auto">
        <div class="default">Type an email or list name</div>
        <ul class="feed">
          <li>fred@foo.bar</li>
          <li>barney@foo.bar</li>
        </ul>
      </div>
    </li></ol>
  </form>
  <script>
    $(function() {
      var tbl = $('#test-1').textboxlist().get(0);
      //tbl.add('added@here.com');
      //tbl.autoFeed('autofeed1@there.com');
      //tbl.autoFeed('autofeed2@there.com');
      //tbl.autoFeed('autofeed3@there.com');
      $.getJSON('json.html', function(json) {
        $.each(json, function(idx,str) { tbl.autoFeed(str); });
      });
    });
  </script>
  <div id="console">
  </div>
</body>
</html>

17 Comments

Glad to see a jQuery port at last, since so many looked for one. I’ll add a link to this in my blog later!

Posted by Guillermo Rauch on 21 October 2008 @ 4am

guillermo himself! big ups, young man. your parents must be proud.

Posted by doug on 21 October 2008 @ 5am

in the honor of transformers everywhere, how about … “autobox”

Posted by doug on 21 October 2008 @ 5am

Hi,

Great script. Is it possible to assign a value, like an ID, for each element?
Will be easier in much case to fetch an ID than a plain text.

Thanks for your work.

Cedric

Posted by Cedric on 22 October 2008 @ 6am

additional note: works fine on Moz + Safari for Mac
And you could have the demo work out of the box if you replaced the line that calls for a non-existant jQuery local version by that
[script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js" type="text/javascript"]

Posted by Cedric on 22 October 2008 @ 7am

Whow, awesome!!! I was looking for a port for a while now! ;) Great work!

Is there an option to limit the number of elements?

Thanks in advance

Posted by Marco on 22 October 2008 @ 8am

Hi,

I tried the script but couldn’t fetch the values. When submitted, the form just contains nothing.
Any hint?

Thanks

Posted by Dexter Williams on 22 October 2008 @ 11am

hi,
how could be get the value while we submit the form.

please write us back.

srini

Posted by srinivasanm on 22 October 2008 @ 8pm

A feature that i’m missing (but is included in the Guillermo Script): When i’m pressing the backspace key, the last Element (like barney@foo.bar) doesn’t get deleted. Would be nice if you could integrate it

Posted by Marco on 23 October 2008 @ 12am

Hello,
[test results for the jquery autcomplete's alpha version]

It is an good plugin (with alpha version) in jquery. Sure, it will comeup with great plugin if few things are fixed

When i tested the jquery autocomplete in our application, i found the following issues,

1/. Lets take an example, we were searching for an employee and we found that employee in the autocomplete dropdown list.
When we select and close the employee, than we can’t able to search that employee again in the autcomplete text box.
Ex:
Let’s assume we searching for an employee “Ram” and we found the result in the autocomplete dropdown. we select the employee
“Ram” (by clicking in the dropdown) - we found the employee in the textbox. Now, if we close the employee “Ram” from the
textbox and again type the keyword “Ram” - than we can’t able to see the employee “Ram” in the autocomplete dropdown.

2/. How does we can fetch the multiple employers id’s when we submit the form. It means, we need each selected employers
employee code when we submit the form. Right now, i can’t find an way to do this.

3/. Along with DOM,
I most cases in the current world application we will be using DOM/AJAX for developing user friendly applications.
So, i tried with the DOM with the jquery autocomplete plugin.
I will hide the form (jquery autocomplete form), once we click any link than we will show the form in div popup. Here,
i faced many issues. I faced all the above mentioned issues. Along with, i had many other issues with this release.
Please see below,
a/. After displaying the autocomplete form in the div popup, i tried to select few persons from the autocomplete dropdown
and close the divpop up window. Again, i opened the div popup window - but i can see the previously selected persons in the
textboxlist. My question is, how does we can hide or remove the person when we close the popup window.
I tried with this id “test-1″ as document.getElementById(’test-1′).value = NULL. But, there is no result.
b/. In many occasions, when i tested the autocomplete within the div popup — i faced few issues. Once i had selected two
persons and tried to enter the keyword for selecting the third person — i found the already selected two persons were hidden
or not visible in the textbox list.

4/. I would be good, if this plugin supports the UTF-8 charset search. It means, let’s assume the json data consists of the
following data (["Hÿnes","René","Jean-François","Hynes"]).
If we type “Hynes” in the textbox list than it should search for the “Hÿnes” and also “Hynes” and many more if it matches
in the json data.
I know we can do this via Regex, but it would make this jquery plugin great if it comes as an inbuilt functionalities.

5/.Also, it would be good to see the autocomplete search results in certain ASC/DESC order — it would be good also to see
that the user can themself adjust the code to change the order. Now, the search results are in some weird order — i couldn’t
guess in which order does it is working.

I found the above issues, when i had implemented and tested in our application.

I hope these issues will be fixed in the next version.

Regards,
Srinivasan M

Posted by srinivasanm on 23 October 2008 @ 2am

Thanks for the comments and reports! New version adds getting the value when submitting, the example has been changed to “test.html” and has an (ugly) submit button.
@Cedric - I replaced the jquery script include in the 0.5.1 version ,thx!
@Dexter,Srinivassan - the input functionality was there but not called but is now fixed as mentioned; use the “separator” option to change the separator string (right now, it’s ‘,’)
@Marco - I took out the backspace functionality explicitly because I would start typing in and backspace too much and delete the previous entry. To make up for it, use left arrow to get to previous entries and delete keys to delete them. I will eventually add validation which can validate for proper email etc. But not sure about number of entries; will think about it.
@Srinivassan - yeah, I know there is weirdness in the autocomplete, thx for your test cases; I will take a closer look at these soon. Also, I will look at what it takes to support UTF-8. Sorting is a great idea which I’ll eventually put in.

Posted by brandon on 23 October 2008 @ 4am

Hello. First of all thanks for this very nice extension.
I just extend little bit functionality by adding trigger for ‘change’ event.
There is my new line.

jquery.autobox.js jquery.autobox\ copy.js
282d281
< $element.trigger(’change’);

So now it’s possible to handle changes in tags by $(’#myinput’).change(function(){/*handling code here..*/});.
But if you need every changes it’s better to put this line to the update() function. I’ve added it here because I need to handle only when used add new entry.

Posted by Vladimir on 8 November 2008 @ 6am

Sorry, one more my change, I forgot about handle change event when user remove entry. So final (for now) changes is

diff jquery.autobox.js jquery.autobox\ copy.js
239d238
< $element.trigger(’change’);
283d281
< $element.trigger(’change’);

Posted by Vladimir on 8 November 2008 @ 7am

HI,

Thanks for the work you had done. This looks great.

This Autocomplete feature cracks down in IE if there are aroung 5000 items in JSON string.
Below is the error its throwing:

Error: Stop running the script

Can anyone please help in resolving this issue?

Many Thanks,
Dheeraj

Posted by Dheeraj on 11 November 2008 @ 10pm

Hello, thanks for the plugin, you’ve made a great work, man! :)

I think I’ve found a bug. It seems that it only find results within the 10 first elements of the json array. I believe it’s because the variable ‘maxresults’, which is set to 10. Maybe I’m missing something but it seems like this variable just limit the search to the n first elements in the array, instead of limit the viewable results. I’ve reproduced this bug in the example you provided and in my own application as well. I hope that this will help you fixing the bug.

Regards,

Pablo

Posted by Pablo on 12 November 2008 @ 6am

It’s realy a good plug-in.
But I found some bug on it:
1. It can’t delete text using BACKSPACE sometimes.
2. The suggest list can’t popup after I deleted some text using BACKSPACE sometimes.
3. When I put this widget into a form with a submit button, it will trigger the submit event when I hit the key “Return”

Posted by robin on 14 November 2008 @ 11am

Btw, guys, this probably don’t work in IE for large database more than 500 items

Posted by Guillermo Rauch on 22 November 2008 @ 9pm

Leave a Comment

I am now a Mac? Coupons everywhere!