Reputation: 101
I have a dropdownList containing profile names of my customers.
As the number of customers is growing I need an autocomplete functionality, so that I am able to look for a particular user with suggestions, and not forced to look for every existing user in the drop down list.
The following code fetches data from the database:
$.getJSON(
"profiles/custoomer.aspx?callback=?",
{},
function (data) {
$.each(data, function (value, name) {
$('<option>').attr('value', value).text(name).appendTo('#customer_profile');
});
}
);
How can I add autocomplete functionality?
Upvotes: 3
Views: 1746
Reputation: 1
$(function() {
var availableTags = ["ribstar","major"];
$( "#search" ).autocomplete({
source: availableTags,
select: function( event, ui )
{
var $this = $(this).val(ui.item.label);
$('#sub_cat').children('[name="'+$this.val()+'"]').attr('selected', true);
}
});
});
and the html part
<div class="ui-widget"><label for="tags">Search: </label><input type="text" name="search" id="search"></div>
<select name="sub_cat" id="sub_cat">
<option value="1" name="ribstar">ribstar</option>
<option value="2" name="major">major</option>
</select>
Upvotes: 0
Reputation: 2428
Try using below example and dont forget to include Jquery latest files ;) Njoy Bro
<script>
$(function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
<div class="demo">
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>
</div><!-- End demo -->
<div style="display: none;" class="demo-description">
<p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are tags for programming languages, give "ja" (for Java or JavaScript) a try.</p>
<p>The datasource is a simple JavaScript array, provided to the widget using the source-option.</p>
</div><!-- End demo-description -->
Upvotes: 0
Reputation: 2016
did you try to use the autocomplete componant ? Here is his documentation, it's easy to use and easy to customize !
http://jqueryui.com/demos/autocomplete/
Upvotes: 1