Example of typeahead:selected event
For anyone interested until an example is added to documentation for custom events...
Assuming there's an input on your page called 'search_input':
$('#search_input').bind('typeahead:selected', function(obj, datum, name) {
alert(JSON.stringify(obj)); // object
// outputs, e.g., {"type":"typeahead:selected","timeStamp":1371822938628,"jQuery19105037956037711017":true,"isTrigger":true,"namespace":"","namespace_re":null,"target":{"jQuery19105037956037711017":46},"delegateTarget":{"jQuery19105037956037711017":46},"currentTarget":
alert(JSON.stringify(datum)); // contains datum value, tokens and custom fields
// outputs, e.g., {"redirect_url":"http://localhost/test/topic/test_topic","image_url":"http://localhost/test/upload/images/t_FWnYhhqd.jpg","description":"A test description","value":"A test value","tokens":["A","test","value"]}
// in this case I created custom fields called 'redirect_url', 'image_url', 'description'
alert(JSON.stringify(name)); // contains dataset name
// outputs, e.g., "my_dataset"
});
https://github.com/twitter/typeahead.js#custom-events
What would be even better would be a pull request that added this to the README :smile: see #174.
Agreed, this should be in the README
Yup, please add this to the documentation.
Thanks, been looking for this :)
Yea, thanks, found this via google after finding docs insufficient.
Nice.... why this is not in the documentation yet?
Wow, been fighting with this thing, thanks A LOT for the info!
:+1: to add it to the readme
@pouipouidesign I've forgotten what this was about aready ;) Can you please spend 5 mins to edit the README & any examples and do a PR?
I will, but over this w-e as I'm in the middle of a sprint of a sort (hence my relief at findings this). I'll just have to pester my husband for info about doing a PR, as I've never done it before :)
I understand that when jharding and I first commented this example didn't have enough attention to make its way to the README, but it's about time it does so, no?
@pouipouidesign I'd be happy to help, I just forgot about the context & don't feel I could correctly represent the issue any more. If you need any help with the PR let me know. I'm insertcoffee on twitter.
Alternatively can people put some info in the comments here and I will compile it in to a PR.
Lot of things will be appreciated in the README and in the exemples like valueKey or tokens usage
There is one more parameter in callback function - dataset name. Can you add it to your example?
Thanks so much! Just what I needed.
@antimony Added dataset name parameter, thanks.
I haven't forgotten to do this, just been swamped. Have good hope of doing it this w-e. Sorry for the delay. If anyone wants to do it faster, please do :)
As a note, I have not been able to find documentation on this and had to add a .off('blur') to the stack to make sure the value does not revert to the selected value.
$('.user-typeahead').typeahead({
remote: '/ajax_user_typeahead?query=%QUERY''
}).bind('typeahead:selected', function(obj, selected, name) {
if (selected.value.toString().split('-').length > 1) { /* "name - [email protected]" */
$(this).val(selected.value.toString().split('-')[0].replace(/^\s+|\s+$/g, '')); /* split, select, trim */
}
return false; /* didn't do anything, maybe not necessary */
}).off('blur');
@wallter if you went to set the value of the typeahead input control, use jQuery#typeahead('val', yourVal). See https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#jquerytypeaheadval-val
I'm trying to get the val of the typeahead instance on selected but I keep getting undefined
$(scope.conf.field).on('typeahead:selected' , function(e,s,d)
{ console.log($(scope.conf.field).typeahead('val'); });
This keeps returning undefined. Anything else inside that event happens fine, just returns undefined for the value of typeahead. Not sure what I've done wrong...
Any reason why you're trying to get the value that way rather than getting it from the suggestion object passed to the event handler i.e. the s argument in your sample code?