Skip to content

Instantly share code, notes, and snippets.

@Radon8472
Last active February 23, 2023 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Radon8472/ee8f504ec7b98b1106f1fc7d7a6b2cfe to your computer and use it in GitHub Desktop.
Save Radon8472/ee8f504ec7b98b1106f1fc7d7a6b2cfe to your computer and use it in GitHub Desktop.
Add custom method to DOM-Element via jQuery
/**
* 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