• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Miscellaneous / Create a one time event handler

Create a one time event handler

Create a one time event handler

Use the once option

When attaching a handler to a given event, you can pass { once: true } to the last parameter of the addEventListener method:

const handler = function(e) {
    // The event handler
};

ele.addEventListener('event-name', handler, { once: true });

Note that this option isn’t supported in IE.

Self-remove the handler

const handler = function(e) {
    // The event handler
    // Do something ...

    // Remove the handler
    e.target.removeEventListener(e.type, handler);
};

ele.addEventListener('event-name', handler);

Source

https://htmldom.dev/create-one-time-event-handler/

Miscellaneous

Related Snippets:

  • How to Get the Query String in JavaScript
  • How To Convert a String to Boolean in JavaScript
  • Determine if a string contains a substring
  • Return all elements in an array except for the first one

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers