Skip to content

Instantly share code, notes, and snippets.

@marcus-at-localhost
Created July 13, 2022 08:41
Show Gist options
  • Select an option

  • Save marcus-at-localhost/426f0915cfcb64bddc0e65651ab2c61f to your computer and use it in GitHub Desktop.

Select an option

Save marcus-at-localhost/426f0915cfcb64bddc0e65651ab2c61f to your computer and use it in GitHub Desktop.
[jQuery to VanillaJS Snippets] #vanillaJS

Cheatsheet

on('click') Event

// jQuery
// -----------
$('.click-me').on('click', function (event) {
	// Do something...
});

// vanillaJS
// -----------
// Listen to all click events on the document
document.addEventListener('click', function (event) {
	// If the clicked element does not have and is not contained by an element with the .click-me class, ignore it
	if (!event.target.closest('.click-me')) return;
	// Otherwise, do something...
});

https://gomakethings.com/how-to-convert-the-jquery-click-method-to-vanilla-js/

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