Run jquery function on element. what is wrong with this code?

What is wrong with this code?

$(function() {
    function testfunction() { $(this).addClass('testing');}
    $('.tester').testfunction();
});
+3
source share
1 answer

testfunction() not added to the jQuery function stack.

If you want to be able to call it on an arbitrary object, you must add it to the jQuery function stack:

$.fn.testfunction = function() {
   this.addClass('testing');
};

$('.tester').testfunction(); // success!

You should look at the jQuery Plugins / Authoring page for more information on how to write plugins correctly.

+13
source

Source: https://habr.com/ru/post/1791955/


All Articles