How to get the input element triggering the jQuery autocomplete widget?
Asked Answered
U

5

21

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
    }
});
Unclasp answered 9/10, 2012 at 21:42 Comment(0)
A
13

Try using $(this)

$('input[type=text]').autocomplete({
    source: 'suggestions.php',
    select: function(event, ui) {
        // here I need the input element that have triggered the event
        alert($(this).attr('name'));
    }
});
Auten answered 9/10, 2012 at 21:43 Comment(1)
Thanks, that's it! I was trying $(this).name wondering why it is undefined. :)Unclasp
D
36

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);
            }
        });
    }
});
Divisor answered 23/10, 2014 at 7:38 Comment(3)
Thanks this is what I needed too. It is particularly useful when you want to pass the autocomplete input field's attribute(s) as parameters to your target lookup script.Paiz
The queried value actually resides in request.term. At least I use this all the time.Avocation
Good support, by the way where is the documentation.Hexangular
A
13

Try using $(this)

$('input[type=text]').autocomplete({
    source: 'suggestions.php',
    select: function(event, ui) {
        // here I need the input element that have triggered the event
        alert($(this).attr('name'));
    }
});
Auten answered 9/10, 2012 at 21:43 Comment(1)
Thanks, that's it! I was trying $(this).name wondering why it is undefined. :)Unclasp
C
4

You can use $(this) or event.target.

Then you can get the name using $(this).prop('name') or event.target.name.

demo

Confute answered 9/10, 2012 at 21:46 Comment(1)
+1 For the event.target. Was a bit skeptical about using $(this) (closure and such). But both work and event.target is new information for me.Banting
D
3

Use $(this)

 select: function(event, ui) {
        $(this).attr('name');
    }
Diplomatic answered 9/10, 2012 at 21:45 Comment(0)
D
3

As far as I can tell for this example, you wouldn't need the $(). this would work just fine since name is a defined attribute.

this.name

Dc answered 9/10, 2012 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.