In this tutorial, we'll learn how to create and use custom jQuery plugins. A jQuery plugin is simply a new method that we use to extend jQuery's prototype object. By extending the prototype object, you enable all jQuery objects to inherit any methods that you add.
Goals for this tutorial:
- Understand the structure of a jQuery plugin.
- Learn how to encapsulate functionality in a reusable way.
- Develop a custom jQuery plugin.
What you will learn:
- Plugin creation basics
- How to pass options to plugins
- How to protect jQuery's alias and plugin's alias
- How to make your plugin customizable
Prerequisites:
- Basic knowledge of JavaScript
- Basic understanding of jQuery
A basic jQuery plugin structure looks as follows:
(function($){
$.fn.myPlugin = function() {
// Plugin functionality goes here
};
})(jQuery);
This is known as an Immediately Invoked Function Expression (IIFE) and it helps to prevent conflicts with other scripts.
Let's create a simple plugin that changes the text color of an element.
(function($){
$.fn.changeColor = function(color) {
this.css('color', color);
return this;
};
})(jQuery);
To use this plugin, you simply call it like any other jQuery method:
$('p').changeColor('blue');
This will change the color of all paragraph texts to blue.
In this example, we'll create a plugin that lets you change multiple CSS properties at once and allows you to pass options to it.
(function($){
$.fn.styleElement = function(options) {
// Default options
var settings = $.extend({
color: 'black',
backgroundColor: 'white'
}, options );
// Apply CSS properties
this.css({
color: settings.color,
backgroundColor: settings.backgroundColor
});
return this;
};
})(jQuery);
To use this plugin with custom options, you can do:
$('p').styleElement({color: 'red', backgroundColor: 'yellow'});
It's a good practice to pass the $
symbol as an argument to your Immediately Invoked Function Expression (IIFE) to avoid conflicts with other libraries.
(function($){
$.fn.myPlugin = function() {
// Plugin functionality goes here
};
})(jQuery);
In this tutorial, we learned the basics of jQuery plugin creation, how to pass options to plugins, how to protect jQuery's alias and plugin's alias, and how to make plugins customizable.
For further learning, you can explore how to handle events, create callback functions, and add additional methods in your plugins.
Exercise 1: Create a jQuery plugin that changes the font size of text.
Solution:
(function($){
$.fn.changeFontSize = function(size) {
this.css('font-size', size);
return this;
};
})(jQuery);
$('p').changeFontSize('20px');
Exercise 2: Create a jQuery plugin that changes the color and font size of text and accepts options for both properties.
Solution:
(function($){
$.fn.styleText = function(options) {
var settings = $.extend({
color: 'black',
fontSize: '16px'
}, options );
this.css({
color: settings.color,
fontSize: settings.fontSize
});
return this;
};
})(jQuery);
$('p').styleText({color: 'red', fontSize: '25px'});
Keep practicing to understand the concept more clearly and try creating different types of plugins. Happy Coding!