Skip to content

Instantly share code, notes, and snippets.

@megclaypool
Last active February 27, 2024 23:39
Show Gist options
  • Save megclaypool/4431efd05037b4b5d2fb606273aa3820 to your computer and use it in GitHub Desktop.
Save megclaypool/4431efd05037b4b5d2fb606273aa3820 to your computer and use it in GitHub Desktop.

In Drupal 9.4, jQuery.once() has been removed. However, there's a Javascript function, once(), that can replace it with a little rewriting:

var closeButton = $("#off-canvas .offcanvas__close");
      
closeButton.once().on("click", function () {
  closeOffCanvas();
});

once takes 3 arguments: 1. Arbitrary key, 2. CSS selector, 3. context
It looks like passing a jQuery object as the CSS selector works too 🤷🏽‍♀️

var closeButton = $("#off-canvas .offcanvas__close");

$(once("closeButton", closeButton, context)).on("click", function () {
  closeOffCanvas();
});
$("iframe").once().fitVids({
  customSelector: ['iframe[src*="youtu.be"]'],
});
$(once("iframe", "iframe", context)).fitVids({
  customSelector: ['iframe[src*="youtu.be"]'],
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment