How to create a custom jQuery Plugin ?
Last Updated :
28 Apr, 2025
In this article, we will see the creation of a custom jQuery plugin that will change the background color of an element when the mouse hovers over it. The plugin takes a color as an argument and sets the background color of each element to that color when the element has hovered over. When the mouse leaves the element, the original background color is restored.
Approach 1: The $.fn namespace is used to specify the plugin function, which allows us to enhance jQuery's capabilities with our own custom methods. The plugin function accepts a color as an argument and changes the background color of each element in the set when it is hovered over using jQuery's hover() method. The plugin function additionally saves each element's original background color in a variable so that it can be restored when the mouse leaves the element. Using jQuery's chaining syntax, the plugin function is then called on a group of items.
Example: In this example, we define a custom jQuery plugin function called hoverBackgroundColor. This function takes a color as an argument and sets the background color of each element in the set to that color when the element has hovered over. When the mouse leaves the element, the original background color is restored. We use the plugin on the set of elements with a class of elements by calling $('.my-elements .element').hoverBackgroundColor('red') in the $(function() {...}) block. This sets the background color of each element to red when hovered over.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Custom jQuery Plugin Example</title>
<link rel="stylesheet" href="style.css">
<script src=
"https://code.jquery.com/jquery-3.6.0.min.js">
</script>
<script src="script.js"></script>
</head>
<body>
<h1 style=color:green;>
GeeksforGeeks
</h1>
<h3>Custom jQuery Plugin Example</h3>
<div class="my-elements">
<div class="element">Element 1</div>
<div class="element">Element 2</div>
<div class="element">Element 3</div>
</div>
</body>
</html>
CSS
.my-elements .element {
padding: 10px;
margin-bottom: 10px;
border: 1px solid black;
}
JavaScript
// Define the plugin function
$.fn.hoverBackgroundColor = function (color) {
// Iterate over each element in the set
this.each(function () {
var $this = $(this);
// Set the original background color
var originalColor = $this.css('background-color');
// Change the background color of
// the element on hover
$this.hover(function () {
$(this).css('background-color', color);
}, function () {
$(this).css('background-color', originalColor);
});
});
// Return the jQuery object
// to allow for chaining
return this;
};
// Use the plugin on a set of elements
$(function () {
$('.my-elements .element')
.hoverBackgroundColor('red');
});
Output:
Approach 2: Using CSS Transitions: In this approach, we use CSS transitions to smoothly animate the background color change on hover. We define a transition property in the CSS that specifies a 0.2-second duration and an ease-in-out timing function. Then, in JavaScript, we use the jQuery hover() method to add and remove the 'red' background color on the hover and on the mouse left.
Example: This is another example that illustrates the creation of a custom jQuery Plugin that is utilized to animate & change the background color on hover.
HTML
<!DOCTYPE html>
<html>
<head>
<title>
jQuery Hover Background Color Example
</title>
<link rel="stylesheet" href="style.css">
<script src=
"https://code.jquery.com/jquery-3.6.0.min.js">
</script>
<script src="script.js"></script>
</head>
<body>
<h1 style=color:green;>
GeeksforGeeks
</h1>
<h3>Custom jQuery Plugin Example</h3>
<div class="my-elements">
<div class="element">Element 1</div>
<div class="element">Element 2</div>
<div class="element">Element 3</div>
</div>
</body>
</html>
CSS
.my-elements .element {
padding: 10px;
margin-bottom: 10px;
border: 1px solid black;
transition: background-color 0.2s ease-in-out;
}
JavaScript
$(function () {
$('.my-elements .element').hover(function () {
$(this).css('background-color', 'red');
}, function () {
$(this).css('background-color', '');
});
});
Output:
Similar Reads
How to Customize Bootstrap jQuery Plugins ? Bootstrap is a popular HTML, CSS, and JavaScript framework for building responsive, mobile-first web applications. It includes a library of jQuery plugins that provide various UI components and behaviors, such as modals, tabs, and carousels. While these plugins are useful out of the box, you may nee
4 min read
How to create a jQuery plugin with methods? Jquery is a Javascript library that is built to design or simplifies HTML, CSS, AJAX, DOM as well as event handling for web development. It is Open Source Software and free to all. More than 10 million websites use jquery. Jquery simply converts a line of javascript into methods that can be called i
2 min read
How to check if a jQuery plugin is loaded? There are multiple ways by which we can simply check whether the jQuery plugins are loaded successfully or not using jQuery. We can also check whether a particular function within the plugin is accessible or not. This tutorial will demonstrate how to check if a jQuery plugin is loaded or not. Step 1
3 min read
How to execute jQuery Code ? jQuery is an open-source feature-rich Javascript library that is designed for the simplification of HTML DOM Tree traversal & manipulation, event-handling & CSS animation. It is quite popular for its features like light-weight, fast, small, etc, that make it easier to use. The purpose of usi
4 min read
How to create an HTML element using jQuery ? In this article, we will see how to create an HTML element using jQuery. To create and append the HTML element, we use the jQuery append() method. The jQuery append() method is used to insert some content at the end of the selected elements. Syntax: $(selector).append( content, function(index, html)
2 min read