Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Typeahead - Uncaught TypeError: Cannot read property 'name' of undefined

I am using the Bootstrap-3 Typeahead plugin as well as the Bootstrap Tag Input plugin. I use it like this:

<input type="text" id="name" name="test" data-provide="typeahead">

And the jQuery:

$.get('/design/assets/advertisements/countries.json', function(data) {
    $("#name").typeahead({
        source: data
    });
    $('#name').tagsinput({
        typeahead: {
            source: data
        }
    });
}, 'json');

However, the typeahead and tags works to some point. Whenever I enter some words, i get suggestions, but whenever I've selected a suggestion, whatever I was writing will be added after the tag input.

Example: If I write "Bah" and select "Bahamas", it looks like this: (Where I would believe that it would delete the "Bah")

enter image description here

I get this error - I don't know if that's the reason:

Uncaught TypeError: Cannot read property 'name' of undefined

The error is called to this file: bootstrap3-typeahead.js

like image 267
oliverbj Avatar asked Jun 29 '15 14:06

oliverbj


1 Answers

I've downloaded non-min version of bootstrap-3 typeahead plugin and changed this:

displayText: function(item) {
  return item.name || item;
},

to this

displayText: function(item) {
  if (typeof item == 'undefined') {
    return '';
  }
  return item.name || item;
},

It solved the problem.

Actually I don't know whether it's a bootstrap-3 typeahead plugin bug or some kind of it's incompatibility with bootstrap tags input plugin which I use.

like image 77
Alexander Konotop Avatar answered Nov 17 '22 02:11

Alexander Konotop