JQuery - To be or not to be Posted by Mischa S.

Back

Date: July 18, 2012

Are you getting a JQuery error, that is something like:

TypeError: $(".selector").myfunction is not a function

I was.  And, it was because I hadn't used the magic of .length.  I know, I know, it's not all about length, but in this case, it is.  I had a custom function attached to a selector, that was being called, but I was including the relavent scripts.  So, to prevent this from running, unless I need it, I don't allow the script to run unless the object being selected ACTUALLY exists or, in this case, has length!

if($('.selector').length > 0){
 //rock all the stuff that needs to happen related to this object, but ignore it if it doesn't have any length
}

You may alread know this.  I did, but like most things, I quickly forgot.