Last active
February 23, 2023 09:22
-
-
Save Radon8472/ee8f504ec7b98b1106f1fc7d7a6b2cfe to your computer and use it in GitHub Desktop.
Add custom method to DOM-Element via jQuery
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
/** | |
* improved version of https://stackoverflow.com/a/22649723 | |
* | |
* function is bound to DOM-Element and can even used, when you load the "menu" | |
* via another jQuery selector later in code | |
*/ | |
// Binds the function to each DOM-Element matching my selector | |
const $menu = $('.my-popup').each(function(i) { | |
/** | |
* @param {boolean} state If true (or omitted) it opens the window otherwise it closes | |
*/ | |
this.open = function (state = true) { | |
this.dataset.isOpen = state ? 1 : 0; | |
$(this).toggle( display ); | |
}; | |
this.close = function () { | |
this.open(false); | |
}; | |
}); | |
// to call the function (even if you dont have access to the Original jQuery variable | |
$('.my-popup')[0].open(); | |
$('.my-popup')[0].close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment