Created
April 8, 2021 13:44
-
-
Save dcyoung-dev/c4a4be7f40c6e9d71da0f8b3f1693b56 to your computer and use it in GitHub Desktop.
Create your own custom jQuery functions
This file contains 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
<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