Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: $(...).on is not a function

I am using jQuery litebox. After adding JS and CSS files I got this error TypeError:

$(...).on is not a function at this line in js file                                             "return  $('body').on('click',        'a[rel^=lightbox], area[rel^=lightbox]', function(e) {"  

Can anybody help me to understand the problem here?

I am doing this implementation in CakePHP 1.3.

like image 577
user1613870 Avatar asked Mar 27 '13 22:03

user1613870


People also ask

Is not a function jQuery error?

on if not a function" jQuery error occurs for 2 main reasons: Loading an old version of the jQuery library that doesn't support the on function. Loading a library that overrides the value of the dollar sign $ variable.

Is not a function Typeerror?

This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.

Is not a function JavaScript onclick?

onclick is not a function" error occurs, because there is no onclick() function in jQuery. To solve the error, use the click function instead, e.g. $('#btn'). click(function () {} . Copied!

What is not a function?

A function is a relation in which each input has only one output. In the relation , y is a function of x, because for each input x (1, 2, 3, or 0), there is only one output y. x is not a function of y, because the input y = 3 has multiple outputs: x = 1 and x = 2.


2 Answers

The problem may be if you are using older version of jQuery. Because older versions of jQuery have 'live' method instead of 'on'

like image 179
Shivang Gupta Avatar answered Oct 04 '22 20:10

Shivang Gupta


The usual cause of this is that you're also using Prototype, MooTools, or some other library that makes use of the $ symbol, and you're including that library after jQuery, and so that library is "winning" (taking $ for itself). So the return value of $ isn't a jQuery instance, and so it doesn't have jQuery methods on it (like on).

You can use jQuery with those other libraries, but if you do, you have to use the jQuery symbol rather than its alias $, e.g.:

jQuery('body').on(...); 

And it's usually best if you add this immediately after your script tag including jQuery, before the one including the other library:

<script>jQuery.noConflict();</script> 

...although it's not required if you load the other library after jQuery (it is if you load the other library first).

Using multiple full-function DOM manipulation libraries on the same page isn't ideal, though, just in terms of page weight. So if you can stick with just Prototype/MooTools/whatever or just jQuery, that's usually better.

like image 29
T.J. Crowder Avatar answered Oct 04 '22 21:10

T.J. Crowder