Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery autocomplete - get the id of the autocomplete input field

i have this jQuery code:

$(".autocomplete").autocomplete('url',{
  extraParams: {country: function(){
    var id = $(this).attr("id"); // not working
    var country = id.replace("uni", "country");
    var country_id = $("#"+country).val();
    return country_id;
  }}            
}).result(function(event, item) {
 // getNames(item);
});

As I will have several, ajax loaded forms with autocomplete fields, i need for each of them to send appropriate additional parameters from the respective form to the server.

I was trying to achieve this by getting the id of the input field and using it to get to the id of the country field which will hold the necessary parameter. The id's would be in the appropriate format to allow for this. Something like:

uni_1, country_1; uni_2; country_2

uni being the autocomplete field, and country the additional parameter.

I have realised that $(this).attr("id") will not work inside the autocomplete, as it would with simple jQuery events, and I have not found a way of doing this. I tried using the toSource method to see the autocomplete object, but it seems that it does not hold the field id.

So if anyone has an idea how this could be done, please share.

Thank you very much in advance. Ivan

like image 798
Ivan Avatar asked Feb 25 '11 18:02

Ivan


1 Answers

You can try

$(".autocomplete").each(function() {
  var id = $(this).attr("id");
  $(this).autocomplete('url',{
      extraParams: {country: function(){
        var country = id.replace("uni", "country");
        var country_id = $("#"+country).val();
        return country_id;
      }}            
    }).result(function(event, item) {
     // getNames(item);
    });
});
like image 93
Andrea Avatar answered Sep 29 '22 19:09

Andrea