Justin Meltzer
Justin Meltzer

Reputation: 13548

Why am I getting this JS error?

I get this JS error:

jquery-1.5.1.min.js:16Uncaught TypeError: Cannot set property '_renderItem' of undefined
d.d.extend._Deferred.f.resolveWithjquery-1.5.1.min.js:16
d.d.extend.readyjquery-1.5.1.min.js:16
d.c.addEventListener.A

and it's from this code for the jquery UI autocomplete plugin in my application.js file:

    .data( "autocomplete" )._renderItem = function( ul, item ) {
         return $( "<li></li>" )
           .data( "item.autocomplete", item )
           .append( "<a>" + item.topic.name + "</a>" )
           .appendTo( ul );
            };

I get this code whenever I load a page that does NOT have the text field that the autocomplete code is acting on. Why and how can I get rid of this error?

I'd like to note that although I am getting this error, my application is working normally. Should I even be worrying about this error?

Upvotes: 8

Views: 10526

Answers (4)

al000y
al000y

Reputation: 139

this problem appeared to me when i upgrade the jquery ui from old one to 1.10.0

just change

$('.foo').data("autocomplete")._render...;

To

$('.foo').data("uiAutocomplete")._render...;

source JQueryUI 1.10.0 Autocompleter renderItem problems

Upvotes: 9

nathan gonzalez
nathan gonzalez

Reputation: 11987

why not just wrap the autocomplete code in a check to see if that element exists?

something like this:

if ($'#myElementId').length) {
    $('#myElementId').data( "autocomplete" )._renderItem = function( ul, item ) {
         return $( "<li></li>" )
           .data( "item.autocomplete", item )
           .append( "<a>" + item.topic.name + "</a>" )
           .appendTo( ul );
    };
}

Upvotes: 4

user578895
user578895

Reputation:

$(...).data('autocomplete')

is undefined, and you can't set a property of undefined. try:

var obj = $(...).data('autocomplete');
obj && (obj._renderItem = function(){
   ...
});

Upvotes: 10

Blender
Blender

Reputation: 298206

I think .data("autocomplete") isn't returning an object, as the error says:

Cannot set property '_renderItem' of undefined

If you are doing this:

$('.foo').data("autocomplete")._render...;

You try breaking it up:

$('.foo').data("autocomplete");
$('.foo')._renderItem = ...;

I've never encountered _renderItem, so I'll look more in to that.


Actually, this question seems to explain a problem really similar to your's: Using _renderItem kind of breaks autocomplete field

Upvotes: 0

Related Questions