Programming Web with PHP, CSS, Javascript and ∞
Autocomplete for JQuery.js
Ajax Powered Autocomplete Plugin for JQuery.js
- Icon Notifier (Spinner between Ajax calls)
- Fire onAjaxFailure events when an error occurs during Ajax calls
- Be able to set a custom width
- A more flexible and dynamic creation of the autocomplete text field
The object is pretty complete but it could be nice to include more features. Here some of them:
- Dynamic themes. Being able to change the style of the autocomple and dropdown list could be a really nice feature to have.
- Different effects to show/hide the dropdown list (SlideDown|SlideUp) <- This one is easy with JQuery, any volunteers?
How it works
a) Configure the HEAD section of your HTML page:
<br /> <!-- insert the styles of the autocomplete box --></p> <link rel="stylesheet" href="css/autocomplete.css" type="text/css" media="screen" charset="utf-8" /> <!-- javascript libraries required --> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/autocomplete.jquery.js"></script>
b) Create the objects on DOM Load (you can also create them dynamically too):
var json_options = {
script:'test.php?json=true&limit=6&',
varname:'input',
json:true,
shownoresults:true,
maxresults:16,
callback: function (obj) { $('#json_info').html('you have selected: '+obj.id + ' ' + obj.value + ' (' + obj.info + ')'); }
};
var xml_options = {
script:'test.php?limit=6&', // for the sake of the server test, json var not set
varname:'input',
json:false, // <----------- IMPORTANT
shownoresults:true,
maxresults:16,
callback: function (obj) { $('#xml_info').html('you have selected: '+obj.id + ' ' + obj.value + ' (' + obj.info + ')'); }
};
$(function(){
$('#test_json').autoComplete(json_options); // json test
$('#test_xml').autoComplete(xml_options); // xml test
});
As you can see on the code, the ‘script’ property of the object is where we write the server page to call via AJAX in order to get the dropdown results. Let’s view the important lines within this test.php page:
// here we collect the POSTED variable // what to search? $input = strtolower( $_GET['input'] ); $len = strlen($input); // how many results we display? $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0;
Now, the way you find matches from such POSTED variable is up to you, it could be checked against an array (as in the examples included) or against a database by using the LIKE SQL statement.
Wishlist
Expose your ideas on the comments and I will list them in this small section. It seems that this dropdown keeps rocking, so why not make it better. Here goes mine:
- Paging Results
*note from Miroslav -thanks*
When I use only jQuery plugin autocomplete, it doesn’t work. I have to change the position of div with suggestions by using jQuery .css(“left”,pos.left) and .css(“top”, pos.top + something), otherwise it fails on div.left() and div.top() functions, which aren’t defined.
Reason
This is happening because of the problem that some browsers have with CSS properties not specified as it happens with ‘display:’ style attributes. It is not a bug from the code but from the ‘our beloved browsers’. Thank you very much Miroslav for telling us about this bug.
Updates
14-05-2010 Animations are now using easing functions, linear or swing are available when using jQuery only, more are available from http://gsgd.co.uk/sandbox/jquery/easing/ – Martin Gotik
14-05-2010 Resizing of the list (when more or less items are displayed when typing text) is now animated too, I had to remove and add some elements for it to look smooth in all browsers – Martin Gotik
14-05-2010 The list isn’t hidden anymore when doing an AJAX call when the list is visible, looks more smooth IMHO – Martin Gotik
14-05-2010 Alert in AJAX request is disabled as mentioned by Carpes on the blog – Martin Gotik
05-03-2010 Animations are more dynamic now and available for both the hide and show event – Martin Gotink.
05-03-2010 Updated the browsing through the list with the keyboard. Thanks Martin Gotink.
05-03-2010 MaxResults and MaxHeight implemented. Thanks Martin Gotink.
05-03-2010 Tested all changes with jQuery 1.4.1 and 1.4.2 on IE 6/7/8, Opera 10.10/10.50, FF 3.5 and Chrome 4.0. IE took a bit more effort (especially the maxHeight option), but the result is (almost) the same on all browsers, only IE6/7 has some small issues with the scrollbar when maxHeight is set (some strange effects when using the mouse to drag the scrollbar). Thanks Martin Gotink.
05-03-2010 Tagged all my changes with MG in the .js and .css files in the attached .zip file, so you can see what i’ve changed, and why. Thanks Martin Gotink.
21-01-2010 Fixed style positioning. Thanks Konstantin.
IMPORTANT! All changes made by others will maintain their credits from now on. Apologies for those I didn’t include as I couldn’t update this blog as much as I want due to lack of time -Far too many projects under development.
DOWNLOAD
For some reason, some of you guys cannot download (I have downloads manager plugin for wordpress and it seems that is not working properly with this resource). For those having problems to download, please download the plugin here mirror download
IMPORTANT! Demo is within the download Link -thanks Lev for pointing me this misscommunication thing.
I have updated the link so now all of you shouldnt have problems to download. Sorry for the inconvenience.
|
|
download: Ajax Powered AutoComplete for JQuery (139.34KB) added: 17/01/2010 clicks: 2326 description: |








I'm a 38 year old Web Developer working from Ibiza, Balearic Islands, Spain. I am Founder and Lead Developer for
about 8 months ago
Now maybe it’s just me, but where the heck is the demo for this? I clicked the “demonstration” link from jquery’s website, and have been looking top-to-bottom across this page to see where I can test it out.
On the bright side – your download link is clear and easy to find, which is the reason I didn’t go with my first find (looked nice, but a download link seemed nowhere to be found – at least intuitively).
Anyhow, I’d like to try it out, as I’m looking to start implementing a feature like this into a CMS I develop.
Yours definitely looks (at least from the graphic) much nicer than many others I’ve seen.
My apologies if the demo was smack dab in front of me. I wouldn’t be surprised if it was, as I find myself being able to navigate less and less on websites these days and I’m not sure if it’s the sites or me myself (I’ve got a hunch it’s me).
Peace and kudos on the great looking tool!
about 8 months ago
My apologies Lev, demo is within the downloadable zip file. I should inform the people a bit more.
Thanks and let me know if it works for you. I have implemented this tool on some CMS I have programmed with success.
about 8 months ago
Where is the download? link leads to weird page with news posts. no files.
about 8 months ago
This is very weird… I try to download my self and works!
I will send it forward to you Jeroen…
about 8 months ago
I see what it is… if you click the image, it works. if you click the link, it doesn’t.
about 8 months ago
Try it now Jeroen
Best and thanks
about 8 months ago
Hello Antonio,
Used your autocomplete with prototype, now switched to jquery. Great script !
It works fine with the jquery file included in your zip, but not with the official release downloaded from jquery.com ?
Is that supposed to be like that ? Bug ? Or do I not get something here (probably!) ?
Thanks
about 8 months ago
Hello,
I’m unable to download this plugin (download link: http://ramirezcobos.com/?file_id=4 )
about 8 months ago
Mmm, now it worked … after third try … :0
Some times before it just showed me home page of http://www.ramirezcobos.com/
about 8 months ago
Where can I submit bugreport / or question?
When I use only jQuery plugin autocomplete, i doesn’t work. I have to change the position of div with suggestions by using jQuery .css(“left”,pos.left) and .css(“top”, pos.top + something), otherwise it fails on div.left() and div.top() functions, which aren’t defined.
I didn’t tried the demo with PHP – i don’t have PHP server, but I think its problem not connected with PHP.
about 8 months ago
But the autocomplete is very nice. The best I’ve found.
I hope I can use it in my personal application (not public).
about 8 months ago
Uhm…something wrong with download link…can’t download file. Points to the url: http://ramirezcobos.com/?file_id=4
that’s correct??
tks!
about 8 months ago
wow….ok…works now…clicked the comment link and it’s works….weird… =)
about 7 months ago
I would like to thank you all for the great acceptance of this tool. If *ANY* of you updates the plugin, please share with all of us.
Thanks
about 7 months ago
Can’t get the d/l link to work either. Maybe just post a direct link? Host it on rapidshare or google code or something? Also, what license, if any, are you releasing this under?
about 7 months ago
Managed to d/l it after leaving a comment. Anyways, any idea about a license? Also, the demo doesn’t work for anyone who doesn’t have PHP installed.
about 7 months ago
Guys, I love to help you all so you can have this plugin on your application but, of course that if you don’t have PHP installed it wont work. But that doesn’t mean that if you have ASP, or ASP.net, or whatever your server technology is, you could change the SERVER side page (that is just a demo) and push to the client your own data.
I explained, very well, how you have to PUSH that data from the server, whether JSON or XML. Then, colleagues, be a bit more imaginative… this demo works with PHP but don’t make me change the demo server page to ASP.NET, ASP, JSP, CSP or whatever for you.
Thanks to all
about 7 months ago
By the way… I have changed the link to a direct link to the resource. Hope you don’t have a problem to download it now.
about 7 months ago
License is: DO WHAT YOU WANT A DO AND DO IT WELL. I will be happy to see this app around.
Regards to all
about 7 months ago
Download link not working for me either, but someone mentioned it worked after leaving a comment… I have no idea how that could be but here goes nothing!
about 7 months ago
I can’t download. the link just takes me to a list of articles
about 7 months ago
I fixed some bugs in your code. You can replace line #296
from:
div.left(pos.left).top(pos.top + this.field.height()).width(w).mouseover(function(){self.killTimeout();}).mouseout(function(){self.resetTimeout();});
to this:
div.css(‘left’,pos.left).css(‘top’,pos.top + this.field.height() + this.options.offsety).width(w).mouseover(function(){self.killTimeout();}).mouseout(function(){self.resetTimeout();});
The bug will gone and option offsey will work.
Cheers!
about 7 months ago
I have included a direct link, now there shouldn’t be any problem. Sorry guys, it seems that download manager plugin has some problems.
about 7 months ago
The direct link downloads the jquery.cookie.zip file…. I don’t think this is the autocomplete. Can you fix the direct link? THanks.
about 7 months ago
Hi,
Autocomplete works like a charm, except when the input lookup value has a character ‘#’ in it.
Example: #123456
The autocomplete process does not even initiate/kick-off, inspite of setting minchars to 2 in the options.
Can you please suggest a workaround/hint to resolve this issue.
Appreciate your help.
TIA and Cheers…
about 7 months ago
Hi Mike,
Maybe it is because that the server file (PHP in this case) does not have any array matching ‘#’. Add #123 to the array on the server page and try again my friend.
about 7 months ago
Hi Antonio,
Thanks for your response.
The issue has been resolved.
For some reason, the value was being passed over as ‘#12345 ‘
Please note the extra space being suffixed to the string.
All I had to do was TRIM it and BINGO! ….
Kudos and thanks to you for sharing your excellent “Autcomplete” script.
Cheers….
about 7 months ago
ok, Miroslav pointed that bug and you add the comment which I am totally agree with. However (that is for Miroslav too) we’d like to have a patch for that bug, be it browser’s or not. In particular I use the latest FireFox and have this problem.
Beside that it seems a wonderful tool. Nice work !
about 7 months ago
Hi Jorj,
Update to the bug is done… Let us know if it works for you now…
about 7 months ago
Hi, I have done some testing and cannot see that that results are ever pulled from the cache. Am I doing something wrong or is this a bug?
about 7 months ago
Cache feature is viewed when continuing typing the word and matching one or various elements on the list…
about 7 months ago
Hi Antonio,
Thanks for a great script. To check the cache operation I added an alert – alert(‘Check cache’); – after this line:
if(this.options.cache && (this.nInpC > ol) && l && (l < this.options.maxentries))
which never gets triggered – any thoughts?
about 7 months ago
My fault. I was configuring maxresults not maxentries. What is maxresults for?
about 7 months ago
Hi David,
Is the maximum number of matches you wish to display on the dropdown box.
about 7 months ago
If you are using padding adn/or borders on the element you will not get the correct position for the autocomplete box. You need to use outerWidth and outerHeight (http://api.jquery.com/outerHeight/) which include the border and padding by default. It will include the margin by including ‘true’ for the first argument.
I simply changed this.field.height() to this.field.outerHeight() (on line 297) and this.field.width() to this.field.outerWidth() on lines 286 and 290.
I’m not sure if anyone else is having problems with the file downloaded from the mirror but line 297 shows as:
div.css(?left?,pos.left).css(?top?,pos.top + this.field.height() + this.options.offsety).width(w).mouseover(function(){self.killTimeout();}).mouseout(function(){self.resetTimeout();});
It probably has something to do with file encoding but the question marks should be single or double quotes.
about 7 months ago
hey
thanx for the plugin — two things:
1) I can’t find an api that lists all the required/optional inputs.
2) I can get the styling to look like the picture at the top of the post; it just looks like a default input….thanx..
about 7 months ago
Hi,
I’m having problems with this syntax error that firebug shows:
syntax error
? in autocomplete.jquery.js@218()autoco…uery.js (línea 296)
? in autocomplete.jquery.js@197()autoco…uery.js (línea 216)
? in autocomplete.jquery.js@164()autoco…uery.js (línea 174)
I()jquery.min.js (línea 19)
? in jquery.min.js@19()jquery.min.js (línea 19)
[Break on this error] div.css(?left?,pos.left).css(?to…ut(function(){self.resetTimeout();});\n
Can someone help me to solve it?
Thanks.
about 7 months ago
Hi Mike,
weird behaviour with styling… browser? how do you test it?
api docs not written, i included a demo file that lists all its properties… thx
about 7 months ago
Hi Antonio,
I found some more bugs:
- autocomplete.css in Line 49: background-image: url(../images/autocomplete/ul_corner_tl.gif); — the ../ is missing – google chrome says – i fixed it lokal and now it works
- in google’s chrome the autocompleter does not close on item-select and it is very very slow on typing
Any ideas? Thanks for a soon reply
about 7 months ago
Hi Julian,
I am truly sorry but I wasn’t able to debug google’s chrome behaviour (thank you google to make our lifes easier you too! – why the hell we do need another browser that requires different programming techniques! wasnt enough with IE?)
Anyway, Julian, it would be very kind if you could update us with debugging info as i am at the moment very close to launch a new business and I have no time for that. I will update for google chrome. I promise.
Best
about 7 months ago
Hi Antonio,
ok, i try to find the bug. If there are more, I will send you some more Info. Thanks so far. I hope it will run in Chrome and IE as well… We would like to use it in our rife-share project http://www.mitreisen.net.
Bye from Germany
Julian
about 7 months ago
Hey Antonio,
Works fantastic with a customized codeigniter back-end. One problem I was having was that tab wasn’t firing self.setHighlightedValue(); on the keyup event in IE 8 (didn’t try other versions – though it worked fine in FF).
I copied over the this.field.keyup handler, modified it to this.field.keydown(function(e){… and on the else case added a check for tab with the appropriate call. Works brilliantly. It appears, in at least my version of IE (and two other computers), that tab only fires on the keydown event. You could streamline the following better; but here is a basic fix for anyone who has encountered this. Just append it below this.field.keyup(function(e){…});
this.field.keydown(function(e){
if(!e) e = window.event;
var key = e.keyCode || e.which;
if(key == self.KEY_UP || key == self.KEY_DOWN ) // KEY UP OR KEY DOWN
{
if($(‘#’+this.acID))
self.changeHighlight(key);
}
else{
switch(key)
{
case self.KEY_RETURN:
self.setHighlightedValue();
e.stopPropagation();
break;
case self.KEY_TAB:
self.setHighlightedValue();
break;
case self.KEY_ESC:
self.clearSuggestions();
break;
default:
self.getSuggestions(self.field.val());
break;
}
};
about 7 months ago
Thank you very much Michael!!!
about 7 months ago
Love the script!
Is it possible to have the list remain until the user clicks somewhere else on the page, in the same way that Google does?
about 7 months ago
Yeah… you need to programatically catch the $(document).click… and call the function to close the list
Regards
about 6 months ago
Great work! I love it.
I’m a noob in all of web technologies, but I managed to hook it up and get it (almost) working with django server side.
The problem I have is the pretty autocomplete box opens just below the input box (too high!), and the black triangle (up arrow) is hiding part of the input box.
How can I fix it so that it opens a few pixels lower, like in your screenshot? I suck at CSS hacking
I tried using the outerWidth fix by Shaum, but that didn’t fix it
oh and by the way, as Shaum mentioned, there are some syntax errors in the file (in the fix from Konstantin his quotes are non ascii chars so they got replaced by ‘?’ in your code).
thanks in advance,
Iftah
about 6 months ago
I changed offsety:-5 to offsety:5 in line 14 and that fixed the position problem.
amazing how I try to find an answer for 20 minutes, give up, post a question and then find the answer in 5 seconds.
about 6 months ago
anyone can help me to fix error at line 297 in autocomplete.jquery.js
and how if i want add action if click or enter the result go to another link or ?action
thx
about 6 months ago
therno error display in browser i use firefox 3 and chrome.
i use editor Dreamwevr 8..at line 297 that say an error. so i try to delete :
div.css(?left?,pos.left).css(?top?,pos.top + this.field.height() + this.options.offsety).width(w).mouseover(function(){self.killTimeout();}).mouseout(function(){self.resetTimeout();});
an change to :
div.left(pos.left).top(pos.top + this.field.height()).width(w).mouseover(function(){self.killTimeout();}).mouseout(function(){self.resetTimeout();}); yeez it works
but how?! if i want to read data form mysql and add action go to some link if i click or enter the result from json. depend on the data that i click
thnks
about 6 months ago
The download link isn’t working. Some people mentioned it worked after commenting so I’m trying that.