🚀 Newsletter Sign-up Form ✨

Solution retrospective
I am proud of the JavaScript logic I built for the form's error handling. Instead of just blindly creating a new error message element every time the user clicks submit, I implemented a DOM state check to see if the error already exists. This completely prevents the "spam click" bug and optimizes the browser's memory.
I am also really happy with the custom CSS @keyframes animation I wrote for the success card. Combining opacity and transform: translateY gave the component a premium slide-up entrance that feels much more natural than a sudden layout shift.
The "Spam-Click" Error Message Bug
The Challenge: Initially, my JavaScript logic blindly manufactured a brand new <p> element for the error message every single time the user clicked "Submit" with an invalid email. If a user spam-clicked the button, multiple error messages would stack up in the UI before the setTimeout could remove them.
The Solution: I implemented a "Look before you build" DOM state check. Before executing document.createElement, my script uses document.querySelector to check if the error class already exists on the screen. By wrapping the creation logic inside an if (errorAlert = null) statement, the script only build the error message if one isn't already there.
I would love a general code review! Any feedback on my HTML semantics, CSS architecture or JavaScript logic is highly appreciated. Let me know if you see any areas where I could improve or follow better industry best practices!
Please log in to post a comment
Log in with GitHubCommunity feedback
No feedback yet. Be the first to give feedback on Christian Diaz’s solution.
Join our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord