How to manually start a search and then select the first option manually in jQueryUI autocomplete?

How can I run a search and select jQueryUI autocomplete manually? In the example script, I managed to start the search, but the select method cannot be started and select the first option.

JSFIDDLE

The solution in this thread seems deprecated. He gives this error:

Uncaught TypeError: Cannot read property '_trigger' of undefined

$(document).ready(function() {

 var aTags = ["ask","always", "all", "alright", "one", "foo", "blackberry", "tweet","force9", "westerners", "sport"];

 var tag = $('#tags');
 tag.autocomplete({
    source: aTags
 });

 $('div').click(function(){
    var newVal = $(this).text();  
    var oldVal = $('#tags').val();
    tag.val(oldVal+newVal)
    tag.autocomplete('search')
    tag.data('ui-autocomplete')._trigger('select', 'autocompleteselect', {item:{value:tag.val()}});
    
 })

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<div>a</div><div>b</div><div>c</div><div>d</div><div>e</div><div>f</div><div>g</div><div>h</div><div>i</div><div>j</div><div>k</div><div>l</div>
<div>m</div><div>n</div><div>o</div><div>p</div>
<div>q</div><div>r</div><div>s</div><div>t</div><div>u</div><div>v</div><div>w</div><div>x</div>
<div>y</div><div>z</div>

<input type='text' title='Tags' id='tags' />
Run codeHide result
+3
source share
1 answer

( _ ) , . , , , .

data('ui-autocomplete'), , , $(selector).autocomplete('instance') , .

, , , , , ( -, !!!). autocompleteopen.

$(document).ready(function () {
    ...

    tag.on("autocompleteopen", function (event, ui) {
        // If there are suggestion results
        if (items.lenght > 0) {
            // Select the desired item here
        }
    });

    $('div').click(function () {
        var newVal = $(this).text();
        var oldVal = $('#tags').val();
        tag.val(oldVal + newVal);
        tag.autocomplete('search');         
    })
});

tag.autocomplete('instance')._trigger . , , . click .

, , , , , , , .

.

$(document).ready(function() {
  var autoPick = false;
  var aTags = ["ask", "always", "all", "alright", "one", "foo", "blackberry", "tweet", "force9", "westerners", "sport"];
  var tag = $('#tags');
  tag.autocomplete({
    source: aTags
  });
  tag.on("autocompleteopen", function(event, ui) {
    var items = tag.autocomplete('instance').menu.element.children();
    if (items.length > 0 && autoPick) {
      items.eq(0).trigger('click')
      autoPick = false;
    }
  });
  $('div').click(function() {
    var newVal = $(this).text();
    var oldVal = $('#tags').val();
    tag.val(oldVal + newVal);
    autoPick = true;
    tag.autocomplete('search');
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/black-tie/jquery-ui.css">
<div>a</div>
<div>b</div>
<div>c</div>
<div>d</div>
<div>e</div>
<div>f</div>
<div>g</div>
<div>h</div>
<div>i</div>
<div>j</div>
<div>k</div>
<div>l</div>
<div>m</div>
<div>n</div>
<div>o</div>
<div>p</div>
<div>q</div>
<div>r</div>
<div>s</div>
<div>t</div>
<div>u</div>
<div>v</div>
<div>w</div>
<div>x</div>
<div>y</div>
<div>z</div>
<input type='text' title='Tags' id='tags' />
Hide result
+4

Source: https://habr.com/ru/post/1606957/


All Articles