TypeError: $(...).on is not a function
Solution 1
The problem may be if you are using older version of jQuery. Because older versions of jQuery have 'live' method instead of 'on'
Solution 2
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.
Solution 3
This problem is solved, in my case, by encapsulating my jQuery in:
(function($) {
//my jquery
})(jQuery);
Solution 4
If you are using old version of jQuery(< 1.7) then you can use "bind" instead of "on". This will only work in case you are using old version, since as of jQuery 3.0, "bind" has been deprecated.
Admin
Updated on August 17, 2020Comments
-
Admin over 3 years
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.
-
Admin about 11 yearsCrowder, thanks for you guidelines, i am not adding any library before jquery, all the libraries have been added after jquery library. i am using this technique <?php echo $html->css ( 'skin/lightbox.css', 'stylesheet' ); ?> and <?php echo $html->script(array('libs/lightbox'), array('inline' => false)); ?>
-
T.J. Crowder about 11 years@user1613870: As I said, if you're including another library after jQuery (like Prototype or MooTools), you'll have this problem. (Not if you put them in before; if you did them before, you'd have a different problem.) Are you using Prototype or MooTools?
-
Admin over 5 yearsCan you please explain what do you mean by
my jQuery
? do you mean itjQuery version x.x.x
orjQuery cycle.all
or the script you are writing ? -
Admin over 5 yearsYou're a legend given my scenario. Thank you.
-
Levchik about 5 yearsnoConflict did nothing for me... but jQuery('body').on(...); FIXED the problem ... thanks! Will know now :)
-
Jose Paez over 3 yearsI think he meant the script and yes encapsulating my script around that function actually solved my problem