Chaudhary Asghar Ali Chaudhary Asghar Ali - 11 months ago 62
jQuery Question

How can we add our custom function in jquery?

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');
}


and i also want to know that can we rename any function which already available in jquery?

Answer Source

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>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download