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!
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.
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.
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With