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

onSelect option/function #77

Open
gravitymediallc opened this issue Sep 19, 2017 · 1 comment
Open

onSelect option/function #77

gravitymediallc opened this issue Sep 19, 2017 · 1 comment

Comments

@gravitymediallc
Copy link

Hello, I cannot find in your documentation how to handle the onSelect adding of the selection to the fiend when writing a custom function.

For example, in my case, I have an array with objects, and each object has an id, and a name property.

I used the renderItem property/function to create an auto-suggestion div which has data-id and data-name attributes.

Once the suggestion is selected, the function should put the selection's data-name property value as the value of the auto-suggest field, but also, it should change the data-id property of the auto-suggest field to put the item.id property.

The issue is that once I make my own onSelect function, the field no longer gets updated onSelect.

@tymothytym
Copy link

tymothytym commented Sep 20, 2017

You can edit the plugin to pass the input element to the onSelect function ("this" is stored in the variable "that"):

In jquery.auto-complete.js

line 77:

o.onSelect(e, v, item);

to

o.onSelect(e, v, item, that);

line 127:

if (sel.length && that.sc.is(':visible')) { o.onSelect(e, sel.data('val'), sel); setTimeout(function(){ that.sc.hide(); }, 20); }

to

if (sel.length && that.sc.is(':visible')) { o.onSelect(e, sel.data('val'), sel, that); setTimeout(function(){ that.sc.hide(); }, 20); }

line 169:

onSelect: function(e, term, item){}

to

onSelect: function(e, term, item, that){}

You can then get the input element id or a data attribute via standard jQuery:

e.g.

onSelect: function(event, term, item, that) {
    // the input's id
    console.log('this id = ', that.attr('id'));
    // set "data-id" on the input to the id of the selected item
    that.attr('data-id', item.attr('id'));
 }

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

2 participants