8

How does one go about making a custom function to append to the jQuery selector? Something that would look like:

$('.my_class').my_function();

1 Answer 1

11

You want to add the function to jQuery.fn (which is a reference to the jQuery object prototype). For more details check out:

http://docs.jquery.com/Plugins/Authoring

An example\stub:

(function($){
  $.fn.my_function = function(){ ... };

})(jQuery);
Sign up to request clarification or add additional context in comments.

3 Comments

i'm willing to bet this question is a duplicate :-\
I would have thought so too, which is why I did check first :)
This is great! D'ya by any chance know of a vanilla.js way of writing this so that if your project doesn't have JQuery, it won't throw any errors? I realise that it won't work either, but I want to add it to the "universal-functions.js" file that I throw into all my projects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.