I'm building an application and I would like to create a common occurrence Autocomplete ... in most places I have an autocomplete the same word, but depending on where the selection has to do different things ...

I am forced to create multiple events like this:

$(document).on('focus', '.element1', function(){

varrif= $(this).attr('rif');

varurl = 'menu/switch.php';

varact = 'farmCompl';

$( this ).autocomplete({

minLength: 3,

source: function( request, response ) {

ajaxComplete (request, response, url, act);

},

select: function( event, ui ) {

//action 1

}

});

});

$(document).on('focus', '.element2', function(){

...

select: function( event, ui ) {

//action 2

}

});

});

or I can create a function of autocomplete that returns the values ​​found? like this:

functionautoComp(rif) {

varurl = 'menu/switch.php';

varact = 'farmCompl';

$( '.'+rif ).autocomplete({

minLength: 3,

source: function( request, response ) {

ajaxComplete (request, response, url, act);

},

select: function( event, ui ) {

varobj = {};

obj[0] = ui.item.id;

obj[1] = ui.item.value;

return obj;

}

});

}

$(document).on('focus', '.event1', function(){

varrif= $(this).attr('rif');

obj = autoComp(rif);

//action1

});

$(document).on('focus', '.event2', function(){

varrif= $(this).attr('rif');

obj = autoComp(rif);

//action2

});

The second case is better for me.... but I've no return from the funtion