Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a custom jQuery Plugin?
To create a jQuery plugin, firstly create a file named jquery-myplugin.js with the following code. This is our plugin code. Here, myplugin is the name of our plugin.
(function ( $ ) {
$.fn.myplugin = function( options ) {
var web = $.extend({
name: 'tutorialspoint'
}, options );
return this.append('Website: ' + web.name + '.com');
};
}( jQuery ));
Now, to load it, add to the HTML file, new.html:
<html>
<head>
<script src="https://www.tutorialspoint.com/%3Ca%20href="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" rel="nofollow" target="_blank">https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://www.tutorialspoint.com/jquery-myplugin.js"></script>
<script>
$(document).ready(function() {
$('#myclass').myplugin();
});
</script>
</head>
<body>
<div id="myclass"></div>
</body>
</html>
Run the above code and you can see the text is visible, which shows your custom plugin is working correctly.
Advertisements