Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unwanted behaviour when using $('.typeahead').typeahead('val', myVal); #646

Closed
8enSmith opened this issue Feb 6, 2014 · 12 comments
Closed

Comments

@8enSmith
Copy link

8enSmith commented Feb 6, 2014

Whilst answering a StackOverflow question I found what I deem to be undesirable behaviour with the "$('.typeahead').typeahead('val', myVal);" function.

When this function is called it causes "myVal" to be correctly inserted, however it causes the suggestion box to also appear. I think users will expect this function to just populate the typeahead control (i.e. just like a call to $('.typeahead').val(myVal);)).

See this jsFiddle and uncomment the call to typeahead('val', ...) to recreate the issue.

Is this indeed an issue or is it expected behaviour?

@jharding
Copy link
Contributor

jharding commented Feb 6, 2014

I agree that this isn't ideal. I think the correct behavior should be dependent on whether the dropdown menu is open or closed. If it is closed, it should just update the input control. If it is opened, it should update the input control and request/render the corresponding suggestions. Does that sound reasonable?

@8enSmith
Copy link
Author

8enSmith commented Feb 6, 2014

Yes, what you have proposed above sounds ideal.

@jharding jharding added this to the v0.10.2 milestone Feb 7, 2014
@jharding
Copy link
Contributor

Linking to #402 as it's a similar issue.

@koraktor
Copy link
Contributor

I found a workaround for this problem:

var typeaheadInput= $('.typeahead').data('ttTypeahead').input;
typeaheadInput.setQuery(myVal);
typeaheadInput.resetInputValue();

@jbarata
Copy link

jbarata commented Feb 20, 2014

Hi,

another workaround that seems to work is to use jquery val
$('.typeahead').val('the value')

although the docs advise otherwise (https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#api)

@jharding what's exactly the problem with this approach?

Thanks.

@jharding
Copy link
Contributor

The typeahead jQuery plugin cannot detect when you manually set the value of the input, so there's a chance some internal state can get out of sync when you do this. I should actually verify this though as quite a bit has changed since I initially recommended that.

@gpbl
Copy link

gpbl commented Feb 27, 2014

Hello – I can confirm the state gets out of sync when using $('.typeahead').val('the value').
@koraktor's workaround worked for me (thanks!)

@yanickrochon
Copy link

I am unsure how this behavior was resolved. I just installed this component and used .typeahead('val', newValue); to manually set the field's value and the suggestion menu opens.

Right now, I resolve this using

el.typeahead('val', newValue).one('typeahead:open', function () {
  el.typeahead('close');
});

@yanickrochon
Copy link

Also, even if the suggestion list is closed, I find that setting some items cause the autocomplete to show a hint when it's not supposed to.

For example :

el.typeahead('val', 'foo');

Will set the correct value "foo" inside my field (where my datasource display is set to return item.name), but if I have a remote item like { name: 'foobar' } then I'll see "foobar" in my field as "suggestion" even though the actual value properly set is "foo".

How do I set a value without triggering a search, suggesitons, and autocomplete?

@yanickrochon
Copy link

This should be easy to solve if I could pass silent to the setVal function

el.typeahead('val', 'foo', true);

@yanickrochon
Copy link

Well, instead of using val, I'm using this and it works as expected.

el.data('ttTypeahead').input.setQuery('foo', true);

@danielsedlacek
Copy link

Also the documentation could be less ambiguous, it is not clear it the typeahead ("val",val) will only set the value or trigger the search too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

7 participants