Skip to content

Instantly share code, notes, and snippets.

@luisenriquecorona
Created June 7, 2019 00:39
Show Gist options
  • Save luisenriquecorona/e9fcc6aa96658897ae90e469f00c2cac to your computer and use it in GitHub Desktop.
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
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;
};
(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