Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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