Created
June 7, 2019 00:39
-
-
Save luisenriquecorona/e9fcc6aa96658897ae90e469f00c2cac to your computer and use it in GitHub Desktop.
It is almost trivially easy to write your own jQuery extensions. The trick is to know that jQuery.fn is the prototype object for all jQuery objects. If you add a function to this object, that function becomes a jQuery method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery.fn.println = function() { | |
// Join all the arguments into a space-separated string | |
var msg = Array.prototype.join.call(arguments, " "); | |
// Loop through each element in the jQuery object | |
this.each(function() { | |
// For each one, append the string as plain text, then append a <br/>. | |
jQuery(this).append(document.createTextNode(msg)).append("<br/>"); | |
}); | |
// Return the unmodified jQuery object for method chaining | |
return this; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { // An anonymous function with one parameter named $ | |
// Put your plugin code here | |
}(jQuery)); // Invoke the function with the jQuery object as its argument |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment