Fire Onchange event after AutoComplete

user1811989 picture user1811989 · Nov 28, 2012 · Viewed 54.2k times · Source

I am trying to do Autocomplete and onchange event at a time.

Means:I want to fire onchange event in a autocomplete textbox. When i write something in the a textbox from keyboard and click outside the textbox then onchange event firing but when i select something in the from the auto suggest names onchange event is not firing.

My HTML Code

<div style="width: 34%">
   Person Name:<input id="txt0" type="text" onchange="SaveData('txt0')" class="userSearch" placeholder="Helped Person" />
</div>

JavaScript Code

function AutoComplete() {
//Autocomplete added to the textbox.
$('.userSearch').autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "CTC.aspx/GetMemberName",
            data: "{ 'prefixText': '" + request.term + "' }",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                response(data.d)
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert('Error occured while autocomplete');
            }
        });
    },
    minLength: 1
});
}

function SaveData(textBoxId) {
     alert(textBoxId);
}

I want to fire the onchange event after selected from autocomplete.

Please Help

Thanks in advance.

Answer

kdrvn picture kdrvn · Nov 28, 2012

You can use the change event on the autocomplete widget to achieve this. http://api.jqueryui.com/autocomplete/#event-change

For example:

function AutoComplete() {
  //Autocomplete added to the textbox.
  $('.userSearch').autocomplete({
      source: function (request, response) {
         // your ajax code
      },
      minLength: 1,
      change: function (event, ui) { SaveData(); }
  });
}