jqueryjquery-autocompletejquery-widgets

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


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
    }
});

Solution

  • 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'));
        }
    });