Skip to content

Instantly share code, notes, and snippets.

@dcyoung-dev
Created April 8, 2021 13:44
Show Gist options
  • Save dcyoung-dev/c4a4be7f40c6e9d71da0f8b3f1693b56 to your computer and use it in GitHub Desktop.
Save dcyoung-dev/c4a4be7f40c6e9d71da0f8b3f1693b56 to your computer and use it in GitHub Desktop.
Create your own custom jQuery functions
<script>
// Declaring a new jQuery function
(function ($) {
$.fn.newSuperCoolFunction = function () {
return this.each(function (_, selector) {
var elements = $(selector)
elements.each(function (_, elementSelector) {
var element = $(elementSelector);
element.addClass('super-cool')
});
})
}
})(jQuery)
// Call the new function
$(document).ready(function() {
$('button').newSuperCoolFunction();
});
</script>
<button>
Make me super cool
</button>
<button>
Make me super cool
</button>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment