Skip to content

Instantly share code, notes, and snippets.

@whoisryosuke
Last active October 13, 2023 10:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whoisryosuke/00ec453a7d4f54089813c35016a9e799 to your computer and use it in GitHub Desktop.
Save whoisryosuke/00ec453a7d4f54089813c35016a9e799 to your computer and use it in GitHub Desktop.
React/JS - How to target "parent" instead of child during onClick (problem: e.target refs actual child clicked, instead of onClick wrapper)
const submitForm = (e) => {
// ❌ Targets child
console.log(e.target.value)
// ✅ Targets wrapper with onClick prop
console.log(e.currentTarget.value)
// ✅ If above fails, log currentTarget and see if it looks like HTML
// currentTarget usually returns as type of HTMLElement
// If so, use this to grabs attributes (like href or name)
// see: https://stackoverflow.com/q/29223502/10097916
console.log(e.currentTarget.getAttribute('href'))
}
@SalahSoussou
Copy link

thank you :)

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