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

Detect when the input value changed if the user clicked a suggestion or not #6

Closed
lmammino opened this issue Sep 6, 2012 · 3 comments

Comments

@lmammino
Copy link

lmammino commented Sep 6, 2012

Is there a built-in way to detect, when the input changes, whether the user clicked a suggestion or not?

I looked at the code and i found the field value is set before calling the onSelect callback.

            //...
    select: function (i) {
        var selectedValue, f;
        selectedValue = this.suggestions[i];
        if (selectedValue) {
            this.el.val(selectedValue);
            if (this.options.autoSubmit) {
                f = this.el.parents('form');
                if (f.length > 0) { f.get(0).submit(); }
            }
            this.ignoreValueChange = true;
            this.hide();
            this.onSelect(i);
        }
    },
           //...

If the onSelect is called before the this.el.val(selectedValue) one would easily detect if the user selected a suggestion on change doing something like this:

$('.autocomplete').autocomplete({
    serviceUrl : '/ajax/autocomplete',
    onSelect : function(value, data, input)
    {
        input.data('suggestionClicked', true);
    }
});

$('.autocomplete').change(function(){
    if($(this).data('suggestionClicked')
    {
          // ...
    }
});

Please let me know if there's a built-in proper way to do so

Thanks

@ghost
Copy link

ghost commented Sep 6, 2012

Hello Luciano,

There two methods on of them is onSelect and another is onChange

onSelect: function (value, data) {

},
onChange: function(value, data) {
if (data == -1) {
…..
}
},

Thanks

From: Luciano Mammino [mailto:notifications@github.com]
Sent: Thursday, September 06, 2012 4:36 PM
To: devbridge/jQuery-Autocomplete
Subject: [jQuery-Autocomplete] Detect when the input value changed if the user clicked a suggestion or not (#6)

Is there a built-in way to detect, when the input changes, whether the user clicked a suggestion or not?

I looked at the code and i found the field value is set before calling the onSelect callback.

        //...

select: function (i) {

    var selectedValue, f;

    selectedValue = this.suggestions[i];

    if (selectedValue) {

        this.el.val(selectedValue);

        if (this.options.autoSubmit) {

            f = this.el.parents('form');

            if (f.length > 0) { f.get(0).submit(); }

        }

        this.ignoreValueChange = true;

        this.hide();

        this.onSelect(i);

    }

},

       //...

If the onSelect is called before the this.el.val(selectedValue) one would easily detect if the user selected a suggestion on change doing something like this:

$('.autocomplete').autocomplete({

serviceUrl : '/ajax/autocomplete',

onSelect : function(value, data, input)

{

    input.data('suggestionClicked', true);

}

});

$('.autocomplete').change(function(){

if($(this).data('suggestionClicked')

{

      // ...

}

});

Please let me know if there's a built-in proper way to do so

Thanks


Reply to this email directly or view it on GitHubhttps://github.com//issues/6.

@lmammino
Copy link
Author

lmammino commented Sep 6, 2012

Thanks @Vytautas-Pranskunas .
Anyway the onChange calback is just a wrapper for the input default change event callback.
I mean It's called before the onSelect... so my problem persists...

@lmammino
Copy link
Author

lmammino commented Sep 7, 2012

Now i digged further into your code and discovered that your onChange function will pass a -1 as value for the data parameter when nothing has been selected. So this way I'm able to detect whether the user selected a suggestion or not.

Closing this issue. Thanks anyway!

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

No branches or pull requests

1 participant