Last active
October 13, 2023 10:47
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you :)