简体   繁体   中英

Get jquery autocomplete input

$(function() {
    $(window).scroll(doSomething);
    $(window).resize(doSomething);
});

function doSomething()
{
    var autocomplete = $(".ui-autocomplete:visible");
    // How can I get input DOM element of currently shown autocomplete there?
}

How can I get input DOM element of currently shown autocomplete if I am outside of the scope of autocomplete declaration?

My solution:

var currentAutocompleteTextbox = null; //global variable

$(textbox).focus(function () { currentAutocompleteTextbox = textbox; });
$(textbox).autocomplete({ ... });

Can anyone propose a better way?

Your goal is unclear. If all you want is to get the last interacted autocomplete textbox your best bet is to use one of the autocomplete plugin events. Below is sample code which uses the focus event. The callback function will execute with the corresponding input textbox as context; therefore this will point to that textbox.

$(".language").autocomplete({
        source: availableTags,
        focus: function(evt, ui){
            var txtbx = $(this).parent().prev().text();
            $('#result').html(txtbx + ' textbox last focused');
        }
    });

Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM