Click here to Skip to main content
Click here to Skip to main content

jQuery UI Autocomplete with ID

By , 1 Feb 2011
 

JQuery_UI_logo_color_onwhite-300x72.png

Introduction

The jQuery UI auto-complete widget is great. Basically, it just works and is very easy to customize.

The Problem

By default, this control supports the retrieval of Text and Value for the selected item but still needs a tweak or two.

What I'm about to show here is how to:

  • Use the basics of the control
  • Use and store the item ID
  • Don't show the ID on the textbox while selecting an item

The Basics

The basic idea is to pass a JSON array of strings and it will do the rest.

var req = ["item 1", "item 2", "item 3"];
$('#txtRequestor').autocomplete({ source: req });

Storing the selected item ID

In the real world, nothing is this simple, and we usually have an ID for each item on the list. We may use this ID in several ways, but the more obvious method is to store the selected ID on the database. This is still very simple, just feed the control some JSON objects instead of simple strings.

Note that each item must at least have the following structure:

  • label - Represents the display text
  • value - Represents the item value (the ID in our case)
var req = [
  {"label":"item 1", "value":1}, 
  {"label":"item 2", "value":2}, 
  {"label":"item 3", "value":3}];

$('#txtRequestor').autocomplete({ 
     source: req,
     change: function (event, ui) { alert(ui.item.value); } });

Using the change event, we can handle the selected item ID and store it in a variable for later use.

Not showing the value (ID) on the textbox

If you have tried the code above, you would have got annoyed with the fact that our ID is being shown on the textbox while we select an item from the list. Haven't you?! :)

To avoid this is very simple, we must change our JSON object a little as follows:

var req = [
  {"label":"item 1", "value":"item 1", "id": 1}, 
  {"label":"item 2", "value":"item 2", "id": 2}, 
  {"label":"item 3", "value":"item 1", "id": 3}];

$('#txtRequestor').autocomplete({ 
     source: req,
     change: function (event, ui) { alert(ui.item.id); } });

As you can see, I just added a new property to each item for our ID value. This will foul the control passing the same value as both display and value, and have a "backup" property that holds the actual ID value.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

AlexCode
Architect
Switzerland Switzerland
Member
Senior IT Consultant working in Switzerland as Software Architect and Developer.
 
Find more at on my blog.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionGreate articlememberSumaNarayanan9 Nov '12 - 22:07 
Really saved lots of my time. Many thanks!!!!!!
Suma

Questionchange event not fire until lost focus the textbox controlmemberYasithl31 Oct '12 - 23:47 
Hi,
 
Here you have used "change" event to get the Id value. But my problem is, change event doesn't occur if the cursor still focus to the textbox control. So is there any suggestion to fix this ?
 
Thanks.
yasith

AnswerRe: change event not fire until lost focus the textbox controlmemberYasithl31 Oct '12 - 23:53 
I think I have found the answer for my own question.
We can use select: function (event, ui) {} instead of "change" event to resolve the issue.
 
Thanks
yasith

GeneralMy vote of 5memberMember 443291817 Sep '12 - 6:13 
USEFULL POST THANK YOU A LOT Big Grin | :-D
GeneralRe: My vote of 5memberAlexCode17 Sep '12 - 20:18 
Thanks mate!
QuestionjQuery-UI-Autocomplete-with-IDmemberMARIANO MOLINA8 Jul '11 - 1:46 
HEllo can help me?
I want to download example jquery-ui-autcomplete because y write the oficial example and can´t do.
thanks
AnswerRe: jQuery-UI-Autocomplete-with-IDmemberAlexCode10 Jul '11 - 14:40 
Hi mate,
I didn't add anything other than the code on this post, so there's nothing to download.
 
The fact is that this is really straightforward, and the last piece of code is a working example.
It's just a matter of how you structure your datasource json to feed the AutiComplete widget.
 
Anyway, tell me what exactly is your problem and I'll try to assist you.
 
Cheers!
Alex
GeneralRe: jQuery-UI-Autocomplete-with-IDmemberMARIANO MOLINA13 Jul '11 - 6:40 
----------
i write this code in c# and not work can help me
i would like with webservice
----------
<head runat="server">
<title></title>
<link href="Styles/jquery-ui-1.8.14.custom.css" rel="stylesheet" type="text/css" />
<script type="text/javascript"
src="Scripts/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="Scripts/jquery-ui-1.8.14.custom.min.js"></script>

<script type="text/javascript">
 
var req = [
{ "label": "item 1", "value": 1 },
{ "label": "item 2", "value": 2 },
{ "label": "item 3", "value": 3}];
 

$('#txtRequestor').autocomplete({
source: req,
change: function (event, ui) { alert(ui.item.value); }
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtRequestor" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 1 Feb 2011
Article Copyright 2011 by AlexCode
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid