Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes TypeError: undefined is not a function?

Tags:

jquery

All, I've got some code that I'm trying to debug and I used the following bit of code in my JS console:

clickEvents = $('ul.breadcrumb[data-allownextonly]').data("events").click;
jQuery.each(clickEvents, function(key, value) {
    console.log(value.handler);
})

When I execute this I get the error: TypeError: undefined is not a function

My question is what causes this to happen? From most of the things I read it deals with the fact that jQuery isn't loaded properly or there is a conflict between two scripts. jQuery is loading properly because I have other objects on my page that jQuery is working fine on. I'm using Wordpress so there is already no conflict.

In my console I changed my JS debugging to something like this:

 clickEvents = jQuery('ul.breadcrumb[data-allownextonly]').data("events").click;
jQuery.each(clickEvents, function(key, value) {
    console.log(value.handler);
})

When I execute this I get the following error: TypeError: Cannot read property 'click' of undefined

Can anyone let me know what other reasons I would be getting these errors? If I have a conflicting script or something along those lines, how can I find out where the conflict is?

Thanks!

like image 266
user1048676 Avatar asked May 31 '12 03:05

user1048676


People also ask

How do I fix TypeError is not a function?

The TypeError: "x" is not a function can be fixed using the following suggestions: Paying attention to detail in code and minimizing typos. Importing the correct and relevant script libraries used in code. Making sure the called property of an object is actually a function.

What is the reason for getting TypeError undefined is not an object?

The Cause. Simply, you're treating a variable like it is an object when it is not. 'undefined' means that your variable hasn't yet been defined (in this case, our object has either not been instantiated or it has been but not assigned to the variable that you are using.

Why is my function saying undefined?

A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned .

What are three reasons why you may see a TypeError?

A TypeError may be thrown when: an operand or argument passed to a function is incompatible with the type expected by that operator or function; or. when attempting to modify a value that cannot be changed; or. when attempting to use a value in an inappropriate way.


1 Answers

According to the jQuery API, the .data( key ) method returns undefined if the matched element doesn't have any data matching the key. Have a look at its usage http://api.jquery.com/data/. It only operates on the first element in the matched set in any case, so you cant use it to get an array of click event handlers. As is, it is saying that the first matched element has no data attribute "events". Once you get the data that is attached, do

var clickEvents = [];
$( selector ).data( key ).each( function() { 
    clickEvents.push( $(this).click );
});

to collect the click events.

like image 184
nbrooks Avatar answered Oct 05 '22 21:10

nbrooks