Suppose there is a script that is initialized by referring to the selector.

$('.autocomplete').autocomplete({ serviceUrl: function() { return ('//autocomplete.travelpayouts.com/jravia?locale=ru&with_countries=false&q=' + $(this).val()); }); 

It has a serviceUrl setting in which the function returns a link. But in order to generate a link, you need to find the value .autocomplete , however, $(this) in the body of the serviceUrl points to the serviceUrl function, and not to the selector itself. So, how can you refer to .autocomplete from the body of the serviceUrl function?

  • one
    " $(this) in the body of serviceUrl points to the function serviceUrl " - you are mistaken, add console.log(this); before return ...; - Igor

1 answer 1

In my opinion, you can:

 $('.autocomplete').each(function() { var $this = $(this); $this.autocomplete({ serviceUrl: function() { return ('//autocomplete.travelpayouts.com/jravia?locale=ru&with_countries=false&q=' + $this.val()); }); }); 
  • And if on the page there are several objects with the class .autocomplete ? - JamesJGoodwin
  • @JamesJGoodwin, slightly changed the answer. So? - Yuri