question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

event to tell when user typed their own value vs selecting a suggestion

See original GitHub issue

Is there a way to get an event when the user “selects” the text that they have typed out, which is separate from the offered suggestions? I.e. user has typed ‘apple’, with provided suggestion ‘applesauce’; however user really wants to submit simply ‘apple’.

Originally I tried using the ‘change’ event for this:

    }).on('typeahead:selected', function($e, datum) {  // suggestion selected
          console.log('selected: ' + datum['url']);
          window.location = datum['url'];
    }).on('change', function(e) {                     // user typed their own value
          value = $('input.search-input').val();
          window.location = '/search_path?search=' + encodeURIComponent(value);
    });

but unfortunately ‘change’ also seems to fire when the user selects from the drop-down, so if the user types ‘apple’ and then clicks ‘applesauce’ there’s a kind of race condition where sometimes the form submits ‘apple’ and sometimes ‘applesauce’, depending on the speed of the remote.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:4
  • Comments:23 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
rprietocommented, Jul 12, 2013

I needed that event too, to display a warning when the user types a “custom” value. It feels pretty hacky looking into “private” data, but here’s my temporary solution:

$('#my-item').on('change', function() {
  var $input = $(this);
  var available = $input.data('ttView').datasets[0].itemHash;
  var datum = _.findWhere(available, {value: $(this).val()});
  if (datum) {
    $input.trigger('typeahead:selected', datum);
  } else {
    $input.trigger('typeahead:uservalue', $(this).val());
  }
});

which I then listen to as:

$('#my-item').on('typeahead:selected',  function(e, datum) { console.log('existing: ' + datum.value); });
$('#my-item').on('typeahead:uservalue', function(e, value) { console.log('new: ' + value); });

This works both when the users selects an existing item, or manually types it out.

0reactions
inviouscommented, Aug 7, 2018

@rprieto $input.data('ttView') is undefined, how do i define datum now?

Read more comments on GitHub >

github_iconTop Results From Across the Web

html - Javascript - Check if suggestions are shown when ...
I'm trying to detect when a user types in an input with autocomplete, if the suggestions are being shown by the browser. For...
Read more >
Event.preventDefault() - Web APIs | MDN
The preventDefault() method of the Event interface tells the user agent that if the event does not get explicitly handled, its default ...
Read more >
Site Search Suggestions - Nielsen Norman Group
When your users select from search suggestions rather than typing in their own complete query, they benefit because they:.
Read more >
Create and edit pivot tables - Google Support
You can add pivot tables based on suggestions in Google Sheets or create them ... Create custom groups—Select the items you want to...
Read more >
Microsoft Privacy Statement
Microsoft collects data from you, through our interactions with you and ... choose not to share personal data, features like personalization that use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found