Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the input element triggering the jQuery autocomplete widget?

Tags:

I have several input fields that are enhanced with jQuery auto-complete functionality. How to get the corresponding input field when a select event is triggered?

<input name="1" value=""> <input name="2" value=""> <input name="3" value="">  $('input[type=text]').autocomplete({     source: 'suggestions.php',     select: function(event, ui) {         // here I need the input element that have triggered the event     } }); 
like image 647
Nikola Obreshkov Avatar asked Oct 09 '12 21:10

Nikola Obreshkov


People also ask

How does jQuery autocomplete work?

In the process of searching a specific value, the jQuery UI autocomplete selection feature provides the user with some string suggestions for the input area which effectively saves time. The autocomplete select action is triggered when the user selects one of the options from the pre-populated list.

What is the default value of Append to option of autocomplete () method?

By default its value is null. This option is used append an element to the menu. By default its value is null. When the value is null, the parents of the input field will be checked for a class of ui-front.

How can create autocomplete search box in jQuery?

Syntax: $("TagId"). autocomplete({ source : itemList // List of Words. })

What is UI autocomplete?

Autocomplete mechanism is frequently used in modern websites to provide the users a list of suggestion while typing the beginning word in the text box. It facilitates the user to select an item from the list, which will be displayed in the input field.


1 Answers

I was using an anonymous function for the source param, and $(this) inside it referenced the funcion, not the element that was triggering it. I had to use $(this.element).

The final code ended similar to this (I simplified it for demo purposes):

$( ".regionsAutocomplete" ).autocomplete({     source: function(request, response){          //The next line is the important one for this example         var input = this.element;         $(input).css('color','red');          var data = {'foo':'bar'};         $.ajax({             'url': ajaxurl,             'data': data,             'method': "POST",             'dataType': "json",             'success': function(data) {                 response(data);             }         });     } }); 
like image 173
Nazareno Lorenzo Avatar answered Oct 19 '22 23:10

Nazareno Lorenzo