Drupal - What's the event triggered after an autocomplete has run and how to retrieve the value selected
No event is triggered at the moment (as of 7.34), but there's a patch on this issue that should let you use something like:
$('#input-id').on('autocompleteSelect', function(event, node) {
});
or if you're using on old version of jQuery
$('#input-id').bind('autocompleteSelect', function(event, node) {
});
Where node
is the selected item. You should be able to get the tid
from one of the properties on that object.
Drupal 7 and 8 provide jQuery autocomplete event generation without any custom code at this time.
In Drupal 7, the autocompleteSelect
event was added in Drupal issue #365241. (Clive mentioned this was in progress when he posted his response.)
Drupal 8 uses the jQuery UI autocomplete widget. The autocompleteclose
event is the jQuery UI event most similar to the D7 autocompleteSelect
event. In D8 the jQuery UI autocompleteselect
event will also be also triggered but an Ajax callback on it will not receive updated form state values. autocompleteclose
callbacks are provided with updated form state values, which is usually what you want.
As the other answers have indicated, you may make use of the event data in the client browser using a jQuery on event handler, or Drupal.behaviors (Drupal 7, Drupal 8). In Drupal 7 you would use the autocompleteSelect
event, and on Drupal 8 autocompleteclose
.
If you need the value in your PHP code, an Ajax callback may be used. Here are some directions for how to do this in Drupal 7 or in Drupal 8.