0

I added an Autocomplete feature to a form on a HTML template, i would like to perform some actions when an hint is selected, is there any way to do it? I'm using Jquery-Typeahead. Here is my actual code:

$(document).ready(function(){

  // Defining the local dataset
  $.getJSON('http://127.0.0.1:8000/myapi', function(data) {
    console.log(data)

    var dt = data

    $(() => {

      $('#myform').typeahead({
        source: {
          data: dt.results.map(record => record.item)
        },
        callback: {
          onInit: function($el) {
            console.log(`Typeahead initiated on: ${$el.prop('tagName')}#${$el.attr('id')}`);
          },
          onClick: function() {
            console.log(); //How can i console.log() the selected value here, for example?
          }
        }
      });

    });

  });
});

1 Answer 1

1

Try defining an onClickAfter callback, it's called right after user clicks on an item. Something like this:

onClickAfter: function(node, a, item, event) {
  // item will be the item you selected
  console.log(item);
}

You can also define the onClickBefore callback the same way, and it will be called immediately before "normal" typeahead behaviour kicks in

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed, it is the right way to go. Thank you a lot Marko!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.