codebrain codebrain - 1 year ago 85
HTML Question

How to set Default value in autocomplete implemented textbox?

I have a textbox implemented with autocomplete. Here is it.

<input id="txtcity" />
source: function (request, response) {
url: '@Url.Action("CityLookup", "PatientDemographic", "")', type: "POST", dataType: "json",
data: { searchText: request.term, maxResults: 10 },
success: function (data) {
response($.map(data, function (item) {
return { label: item.Text, value: item.Text, id: item.Value }
select: function (event, ui) {
open: function () {
close: function () {

This returns three results, let's say "Apple", "Banana", "Cabbage". How do I set a default value to "Banana" while rendering ? i.e, text="banana" value="2"?

Answer Source

you can set the value like this after initializing Autocomplete:

$( "#txtcity" ).autocomplete({
  // do whatever

Updated Example:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download