Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I override the default behavior of select: within jQuery UI Autocomplete?

I have the following:

$('#<%=txtCity.ClientID%>').autocomplete({
  source: function (request, response) {
    var parameters = {
      isoalpha2: '<%=Session["BusinessCountry"].ToString()%>',
      prefixText: request.term
    };
    $.ajax({
      url: '<%=ResolveUrl("~/AtomicService/Assets.asmx/GetCitiesWithState")%>',
      type: 'POST',
      dataType: 'json',
      contentType: 'application/json; charset=utf-8',
      data: JSON.stringify(parameters),
      success: function (data) {
        response($.each(data.d, function (index, value) {
          return {
            label: value.slice(value.indexOf(',')),
            value: parseInt(value.split(',')[0])
          }
        }));
      }

    });
  },
  minLength: 2,
  delay: 50,
  select: function (event, ui) {
    var city = ui.item.label.split(',')[0];
    var state = ui.item.label.split(',')[1];
    alert(city);
    alert(state);
    $('#<%=txtCity.ClientID%>').val(city);
    $('#<%=txtState.ClientID%>').val(state);
  },
});

It's all happy days, except when I select an item from the autocomplete list I'd like to not have autocomplete populate the $('#<%=txtCity.ClientID%>') element. How do I do it? I saw .insertAfter, is that something I should look at?

Help appreciated.

like image 279
dooburt Avatar asked Apr 16 '11 16:04

dooburt


People also ask

What is the default value of Append to option of autocomplete () method?

By default its value is null. This option is used append an element to the menu. By default its value is null. When the value is null, the parents of the input field will be checked for a class of ui-front.

How does jQuery autocomplete work?

In the process of searching a specific value, the jQuery UI autocomplete selection feature provides the user with some string suggestions for the input area which effectively saves time. The autocomplete select action is triggered when the user selects one of the options from the pre-populated list.

How can create autocomplete search box in jQuery?

Syntax: $("TagId"). autocomplete({ source : itemList // List of Words. })

What is autocomplete in js?

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. The datasource is a simple JavaScript array, provided to the widget using the source-option.


1 Answers

Try returning false from the select event:

...
select: function(event, ui) {
    var city = ui.item.label.split(',')[0];
    var state = ui.item.label.split(',')[1];
    alert(city);
    alert(state);
    $('#<%=txtCity.ClientID%>').val(city);
    $('#<%=txtState.ClientID%>').val(state);
    return false;
},

From the documentation (select event):

Triggered when an item is selected from the menu; ui.item refers to the selected item. The default action of select is to replace the text field's value with the value of the selected item. Canceling this event prevents the value from being updated, but does not prevent the menu from closing.

You can try it here.

like image 115
karim79 Avatar answered Oct 27 '22 00:10

karim79