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
The problem may be if you are using older version of jQuery. Because older versions of jQuery have 'live' method instead of 'on'
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.
This problem is solved, in my case, by encapsulating my jQuery in:
(function($) {
//my jquery
})(jQuery);
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.
In my case this code solved my error :
(function (window, document, $) {
'use strict';
var $html = $('html');
$('input[name="myiCheck"]').on('ifClicked', function (event) {
alert("You clicked " + this.value);
});
})(window, document, jQuery);
You don't should put your function inside $(document).ready
I tried the solution of Oskar (and many others) but for me it finaly only worked with:
jQuery(function($){
// Your jQuery code here, using the $
});
See: https://learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/