Skip to content

Use event listener on parent. #2

Description

@QBitFoundry

Use event delegation (usually better)

Attach a single listener to a parent element that already exists:

document.body.addEventListener('click', (event) => {
  if (event.target.matches('.btn')) {
    handleClick(event);
  }
});

Now any current or future .btn element will work automatically.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions