How to create a jQuery plugin with methods?
Last Updated :
20 Dec, 2022
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 in a single line of code. These plugins are implemented. Approach: In Jquery Plug-in is a code that is needed in a standard javascript file. Plugins help in providing different methods that can be used with different jquery library methods.
jQuery.fn.methodName = methodDefinition;
- where .methodname is the name of a method and methoddefinition defines a method
- To Obtain a perfect or compatible code use this.each which is used to perform over a set of matched elements.
- Prefix should conclude with .js.
Syntax: Here is the syntax of a plugin file
(function($){
$.fn.myFunction(settings){
settings = $.extend({setting_A: 'default_A', setting_B: 'default_B'...});
...code away!
};
}(jQuery);
Example 1: Here is a plug-in example.
html
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="jquery-3.4.1.js"
type="text/javascript">
</script>
<script src="custom.js"
type="text/javascript">
</script>
</head>
<script>
(function($) {
$.fn.myFunction(settings) {
settings = $.extend({
setting_A: 'default_A',
setting_B: 'default_B'...
});
...code away!
};
}(jQuery);
</script>
<body>
<h1>GeeksForGeeks</h1>
<p>A Computer Portal For Geeks</p>
<button> Click To Get Started </button>
<script>
$(document).ready(function() {
$('button').click(function() {
alert('My first jquery code');
});
});
</script>
</body>
</html>
Output:
- Before clicking on the button:

- After clicking on the button:

Example 2: The following method is an example of having a warning method. Just Take a look into the syntax of file plugin.js
(function($) {
$.fn.targetBlank = function () {
alert('Working');
}
}) (jQuery);
Now Take a Look into the Example which shows how to link the plugin with HTML
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet"
type="text/css"
href="css/style.css">
</head>
<script>
(function($) {
$.fn.targetBlank = function() {
alert('Working');
}
})(jQuery);
</script>
<body>
<a href="https://www.google.co.in/"
target="_blank">Google</a>
<script type="text/javascript"
src="jquery-3.4.1.js"></script>
<script type="text/javascript"
src="plugin.js"></script>
<script type="text/javascript"
src="ext.js"></script>
</body>
</html>
Output: 
Similar Reads
How To Extend Math.js With Plugins? Math.js is a comprehensive math library for JavaScript and Node.js, designed to handle complex mathematical operations, expressions, and data structures like matrices and units. While Math.js comes with a broad set of built-in functions, there are times when you might need additional functionality d
3 min read
How to create a custom jQuery Plugin ? 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
3 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
jQuery UI Button widget() Method jQuery UI button widget() method returns an object which contains the visual representation of the button Syntax: $( ".selector" ).button("widget") Parameters: This method does not accept any parameters. Return values: This method returns an object value. Links for jQuery UI libraries: <link rel=
1 min read
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