I want to add my custom function in jquery library so that i can access them using $ sign.
I am trying using this way but its not working
$.fn.myFunction = function() {
alert('test');
}
If you are attempting to define a function at jQuery
, or jQuery alias $
use $
without .fn
, which attaches function to an object or selector chained to jQuery()
call.
function myFunction() {
alert("test");
}
jQuery.myFunction = myFunction;
jQuery.myFunction(); // called on `jQuery` object
jQuery.fn.myFunction = myFunction;
jQuery({}).myFunction(); // `$.fn` chained to `jQuery()` call
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>