Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between JQuery plugin and a function

When I look at the JQuery plugin code, it almost seems to me that we are creating a function (although we use the JQuery.fn syntax) Functionally, what's a difference between a function and a plugin?

like image 631
DotnetDude Avatar asked Feb 27 '26 20:02

DotnetDude


1 Answers

A function is just a normal JavaScript function, for example:

function doSomething(param) {
  alert(param);
}

A plugin is intended to be run on a set of elements, for example:

jQuery.fn.plugin = function(param) {
  return this.attr('something', param);
}

This would set the 'something' attribute on all elements it was called on, like this:

$('.selector').plugin('value');

If you intend to use the function on a set of elements, like in a jQuery chain, then a plugin may be the answer for you...if you're just calling a function and doing stuff, really having nothing to do with a set of elements, use a plain named function.

like image 136
Nick Craver Avatar answered Mar 01 '26 08:03

Nick Craver



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!