sw6 - KTBFFH sw6 - KTBFFH - 5 months ago 15
jQuery Question

Execute function on option selected from jQuery ui autocomplete option list

Hi I have the code below for autocomplete which works fine but my only issue is that when a user clicks on an option from the autocomplete list, it should trigger another function (doSomething()). This however is not being done. Granted if the user makes a selection and presses "enter" the function is executed.

var url = "http://myURL";
var field = "myField";

$(document).ready(function () {
$("#tags").autocomplete({
source: function (req, add) {
var suggestions = search(req.term, url, field);
add(suggestions);
},
select: function( event, ui ) {
doSomething();
}
});

});


function search(value, listurl, field) {
var coll = new Array();
var url =
listurl + "?$filter=startswith(" + field + ",'" + value + "')";

$.ajax({
cache: true,
type: "GET",
async: false,
dataType: "json",
url: url,
success: function (data) {
var results = data.d.results;
for (att in results) {
var object = results[att];
for (attt in object) {
if (attt == field) {
coll.push(object[attt]);
}
}
}
}

});
return coll;}

function doSomething() {
}


Thanks for any suggestions.

Answer

Got this resolved like this:

$('#tags').on('keyup change', function () {
        doSomething();
    }).change();

$('#tags').on('autocompleteselect', function (e, ui) {
    doSomething();
    });

Thanks to this SO link

Comments