Home > Programming, jQuery > Using ui autocomplete as a dropdown, with type in

Using ui autocomplete as a dropdown, with type in

I had been using an autocomplete plugin by bassistance. 2 months later jQuery UI added autocomplete to their list of widgets. Like many plugins out there I was unable to find an autocomplete that was easy to use for my purposes. The closest I recently came to was the sexy combo plugin. I cant remember why but it was just a tad short of what I wanted to do, and I’m still quite a javascript novice.

I came up with a way to use the jQuery UI autocomplete widget as a <select> that you could also type in something if the choices in the dropdown didn’t fit your needs.

ui-autocomplete-dropdown

This example uses a local data source, but you can come up with creative ways to use remote JSON sources if you like.

<script type="text/javascript">
	$(document).ready(function() {

		// dropdown/ auto suggest
		$(".ui_dropdown").autocomplete({
			minLength: 0,
			delay: 0
		});

		$(".ui_dropdown").click(function(){

			var input = $(this);
			var inputID = input.attr('id');

			// close if already visible
			if (input.autocomplete("widget").is(":visible")) {
				input.autocomplete("close");
				return false;
			}

			// set source(s)
			var myData = '';
			if(inputID == 'input1'){
				myData = ["foo","bar","hello","world"];
			} else {
				myData = ["1","2","3","4"];
			}

			// load source
			input.autocomplete({
				source: myData
			});

			// fire search event
			input.autocomplete("search", "");
			input.focus();
			return false;
		});		

	});
</script>

In this script I am using the input id’s to define the data source.

Categories: Programming, jQuery Tags:
  1. May 7th, 2010 at 19:50 | #1

    Great information was shared in this post…
    Thank you for the post..

  2. March 16th, 2011 at 20:44 | #2

    So glad they finally rolled this into the core jquery UI. Nice tutorial – just what i was looking for.

  3. April 7th, 2011 at 00:51 | #3

    Thumbs up! You did have a nice tutorial there and I find it informative and so easy to follow. I am going to try it. Thanks!

  4. June 15th, 2011 at 22:55 | #4

    Great information this is excatly what I have been looking for. Thanks heaps

  5. sad123
    October 19th, 2011 at 22:18 | #5

    Mono Bluetooth Wireless Handfree Headset Headphone N909.N909 Mono Bluetooth Wireless Handfree Headset

  6. fdgfds
  7. sdfasdg
  8. fdhgsfh
    October 27th, 2011 at 00:46 | #8

    Up to 6 hours talk time and 8 days standby time
    Status Display makes it easy to check battery level and Bluetooth connectivity at a glance
    Touch volume control, answer/end button, voice dialing (depending on the phone used)
    Streams music from A2DP-enabled mobile phones
    Connect two Bluetooth enabled devices at the same time with Multiuse capabilities Stainless Survival Tool

  9. October 27th, 2011 at 07:43 | #9

    i am facing the problem in this step… but after reading this article i solved my problem… thnaks for sharing thios information..

  10. November 11th, 2011 at 05:33 | #10

    I learn a lot from this post,thank you!

  11. December 6th, 2011 at 01:29 | #11

    Nice, just to let you know the post isnt showing up properly on my iphone – I think there is a plugin you can grab that takes care of that now.

  12. December 9th, 2011 at 04:13 | #12

    Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.

  13. December 15th, 2011 at 02:35 | #13

    This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource

  14. December 16th, 2011 at 22:56 | #14

    THANKYO

  15. January 9th, 2012 at 04:26 | #15
  16. January 11th, 2012 at 04:46 | #16

    Aw, this was a really quality post. In theory I’d like to write like this too – taking time and real effort to make a good article… but what can I say… I procrastinate alot and never seem to get something done.

  17. February 3rd, 2012 at 08:45 | #17

    I found very useful and interesting posts in this blog which is the great signs for a good blogger.

  18. February 3rd, 2012 at 08:46 | #18

    Congratulation for the great post. Those who come to read your article will find lots of helpful and informative tips.

  19. February 3rd, 2012 at 08:48 | #19

    Nice post with awesome points! Can’t wait for the next one.

  20. February 3rd, 2012 at 08:49 | #20

    Nice post with loving snapshot. I am completely impressed! Keep stuff like this coming

  21. February 3rd, 2012 at 08:51 | #21

    Very nice post even i would say that whole blog is awesome. I keep learning new things every day from post like these. Good stuff!

  1. April 1st, 2010 at 10:31 | #1
  2. April 1st, 2010 at 11:10 | #2
  3. April 1st, 2010 at 11:28 | #3
  4. April 1st, 2010 at 11:36 | #4
  5. April 1st, 2010 at 11:50 | #5
  6. April 1st, 2010 at 11:54 | #6
  7. April 1st, 2010 at 12:03 | #7
  8. April 1st, 2010 at 12:09 | #8
  9. April 1st, 2010 at 12:11 | #9
  10. April 1st, 2010 at 12:16 | #10
  11. April 1st, 2010 at 12:27 | #11
  12. April 1st, 2010 at 12:28 | #12
  13. April 1st, 2010 at 12:28 | #13
  14. April 1st, 2010 at 14:09 | #14
  15. April 1st, 2010 at 14:46 | #15
  16. April 1st, 2010 at 14:54 | #16
  17. April 1st, 2010 at 15:24 | #17
  18. April 1st, 2010 at 15:33 | #18
  19. April 8th, 2010 at 15:09 | #19