jQuery: The Write Less, Do More JavaScript Library

Autocomplete - finding out (on blur) if the user has picked an item from the list or not

View: New views
3 Messages — Rating Filter:   Alert me  

Autocomplete - finding out (on blur) if the user has picked an item from the list or not

by gnjunge :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Concerning Jörn Zaefferer autocomplete.

In many cases the autocomplete list is being filled with Key Value
pairs, such as a list of FooBars with their name and id. Using the
result function one would get the id, and drop it into a hidden field.

I want to allow users to either pick an existing FooBars from the
list, or enter a new one if he cannot find a suitable one. But for
that to work I need to know that the autocomplete box contains a
custom item, with other words: an item that was not in the list.

The result event is only triggered whenever an item was picked from
the list.

How would I go about implementing this. My starting point was to add
code to the "blur" event of the autocomplete, yet I can't get enough
information to see if the currentValue is from the list, or a custom
entry.

Thanks,
Gidon

Re: Autocomplete - finding out (on blur) if the user has picked an item from the list or not

by Tom Worster :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 7/2/09 5:49 AM, "gnjunge" <gjunge@...> wrote:

> Concerning Jörn Zaefferer autocomplete.
>
> In many cases the autocomplete list is being filled with Key Value
> pairs, such as a list of FooBars with their name and id. Using the
> result function one would get the id, and drop it into a hidden field.
>
> I want to allow users to either pick an existing FooBars from the
> list, or enter a new one if he cannot find a suitable one. But for
> that to work I need to know that the autocomplete box contains a
> custom item, with other words: an item that was not in the list.
>
> The result event is only triggered whenever an item was picked from
> the list.
>
> How would I go about implementing this. My starting point was to add
> code to the "blur" event of the autocomplete, yet I can't get enough
> information to see if the currentValue is from the list, or a custom
> entry.

what i did was add a flag in the input's .data() called 'chosen'. i set the
flag in the input's .result() handler. i clear it on .change(), or on
.focus() if the input's value is an empty string.

the flag can be read whenever to determine if the value in the field is
chosen from the suggestions or typed.



Re: Autocomplete - finding out (on blur) if the user has picked an item from the list or not

by gnjunge :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Great, thanks for the pointer , I will try that.

On Jul 3, 10:47 pm, Tom Worster <f...@...> wrote:

> On 7/2/09 5:49 AM, "gnjunge" <gju...@...> wrote:
>
>
>
> > Concerning Jörn Zaefferer autocomplete.
>
> > In many cases the autocomplete list is being filled with Key Value
> > pairs, such as a list of FooBars with their name and id. Using the
> > result function one would get the id, and drop it into a hidden field.
>
> > I want to allow users to either pick an existing FooBars from the
> > list, or enter a new one if he cannot find a suitable one. But for
> > that to work I need to know that the autocomplete box contains a
> > custom item, with other words: an item that was not in the list.
>
> > The result event is only triggered whenever an item was picked from
> > the list.
>
> > How would I go about implementing this. My starting point was to add
> > code to the "blur" event of the autocomplete, yet I can't get enough
> > information to see if the currentValue is from the list, or a custom
> > entry.
>
> what i did was add a flag in the input's .data() called 'chosen'. i set the
> flag in the input's .result() handler. i clear it on .change(), or on
> .focus() if the input's value is an empty string.
>
> the flag can be read whenever to determine if the value in the field is
> chosen from the suggestions or typed.