Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 811
Justin Meltzer Avatar asked Apr 08 '11 05:04

Justin Meltzer


People also ask

What does JS error mean?

This happens when a script in a web page contains an error or fails to execute correctly. Exactly what happens depends on the browser and type of error, but in most cases some sort of error notification will be displayed.

How do I fix JavaScript errors in Chrome?

In Chrome, navigate to View > Developer > JavaScript Console or More Tools > JavaScript Console or press Ctrl + Shift + J. The error console will open. If you don't see any errors try reloading the page. The error may be generated when the page loads.


1 Answers

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

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

var obj = $(...).data('autocomplete');
obj && (obj._renderItem = function(){
   ...
});
like image 185
Nobody Avatar answered Oct 12 '22 19:10

Nobody