jQuery-Autocomplete icon indicating copy to clipboard operation
jQuery-Autocomplete copied to clipboard

JQuery autocomplete: Uncaught TypeError: Cannot read property 'value' of undefined

Open ilgala opened this issue 11 years ago • 6 comments

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.

ilgala avatar Nov 19 '15 13:11 ilgala

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.

tkirda avatar Nov 20 '15 03:11 tkirda

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

Neelmore21 avatar Dec 29 '16 06:12 Neelmore21

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;

tkirda avatar Dec 29 '16 15:12 tkirda

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);

Splintex avatar Dec 19 '17 09:12 Splintex

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.

tstizzle avatar Apr 16 '18 23:04 tstizzle

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

nendhruv avatar Apr 17 '18 11:04 nendhruv