Sebastián
Sebastián

Reputation: 95

How can i get the context of a jquery-ui autocomplete widget?

I'm using jQueryUI Autocomplete for a web project. I need get the name attribute of every called input. How can I get it? this can't get the context inside of the function.

$("input").autocomplete({
  delay: 600,
  minLength: 2,
  source: function(request, response) {
    var term = request.term; 
    $.getJSON(url, request, function(data, status, xhr) {
      response(data);
    });
  }
});

Upvotes: 3

Views: 311

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

You can achieve this by initialising the autocomplete inside an each() loop. This will mean you have access to the this reference:

$("input").each(function() {
  var $input = $(this);

  $input.autocomplete({
    delay: 600,
    minLength: 2,
    source: function(request, response) {
      var term = request.term; 
      // do something with $input.prop('name') here...
      $.getJSON(url, request, function(data, status, xhr) {
        response(data);
      });
    } 
  });
});

Upvotes: 4

Related Questions