JQuery autocomplete: Uncaught TypeError: Cannot read property 'value' of undefined
Hi everyone, I'll send you a link to the Stackoverflow question I've made about the error I've titled this issue.
premise I'm using
- JQuery v1.11.0
- devbridge/jQuery-Autocomplete v1.2.24
This is the error I'm facing
Uncaught TypeError: Cannot read property 'value' of undefined
Autocomplete.onSelect @ jquery.autocomplete.min.js:915
Autocomplete.select @ jquery.autocomplete.min.js:850
(anonymous function) @ jquery.autocomplete.min.js:195
n.event.dispatch @ jquery.min.js:3
r.handle @ jquery.min.js:3
To resolve it I've made a change on the Autocomplete source code at the method onSelect: function (index)
I refer to the version 1.2.24
onSelect: function (index) {
var that = this,
onSelectCallback = that.options.onSelect,
suggestion = that.suggestions[index];
that.currentValue = that.getValue(suggestion.value);
if (that.currentValue !== that.el.val() && !that.options.preserveInput) {
that.el.val(that.currentValue);
}
that.signalHint(null);
that.suggestions = [];
that.selection = suggestion;
if ($.isFunction(onSelectCallback)) {
onSelectCallback.call(that.element, suggestion);
}
},
I've changed the method to:
onSelect: function (index) {
var that = this,
onSelectCallback = that.options.onSelect,
suggestion = that.suggestions.length >= 1 ? that.suggestions[index] : that.suggestions;
that.currentValue = that.getValue(suggestion.value);
if (that.currentValue !== that.el.val() && !that.options.preserveInput) {
that.el.val(that.currentValue);
}
that.signalHint(null);
that.suggestions = [];
that.selection = suggestion;
if ($.isFunction(onSelectCallback)) {
onSelectCallback.call(that.element, suggestion);
}
},
This solved my problem. I don't know if any other user is facing the same problem.
Your problem is that you are passing empty suggestion. Probably trailing comma in your suggestions array: [a,b,]. Having correct response that would not happen.
I have got 1 error on my page "Uncaught TypeError: Cannot read property 'label' of undefined". can you please help me to know reason for this error
There is no label in autocomplete code base. What you are doing, is trying to access property on an object that has no value:
// Object has value of undefined
var obj;
// Uncaught TypeError: Cannot read property 'label' of undefined
var label = obj.label;
I have the same problem too. It would be great to check suggestion on undefined before trying to get the value in that.getValue(suggestion.value);
i can confirm this is still an issue in the most recent version. i have verified that the suggestion array is well-formed. and i can confirm that @ilgala 's fix appears to have fixed the issue.
this issue is still there but @ilgala 's solution did not work for me. Removing that.suggestions = []; from onSelect works for me. This does not set back the suggestions to empty and hence it is not undefined on the next click/select