Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
});
like image 632
user247298 Avatar asked Apr 25 '26 17:04

user247298


1 Answers

testfunction() is not added to the jQuery function stack.

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

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

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

You should take a look at jQuery's Plugins/Authoring page for more information on how to properly write plugins.

like image 190
Aron Rotteveel Avatar answered Apr 27 '26 08:04

Aron Rotteveel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!