Design comparison
Solution retrospective
I am a newbie in JavaScript, this form validation is quite hard for me now. I was learning via YouTube video. However, here is one problem I have in JavaScript.
The error icon and error message only shows up the first input, and I guess is the querySeletor only pick up the first element class. But if I use querySelectorAll, it is going to be an array, and it won't work as well. In this case, how can we make it work? Is there any better way to write this code?
For designing part, there is one part I want to optimize. How to make the error icon to align center with placeholder? I use flexbox and align-items to center, but it is not working. If you know how to fix it, please let me know. Appreciate for advance.
Community feedback
- @cholis04Posted over 2 years ago
Hi Pon Huang. Great job on this challenge!
Looks like you're having a little trouble in this challenge. Let's take a look at the problem you're having:
- Error icon and message appear only on first input
All icons and error messages have the same class name. So you need to tell the browser specifically which icon and error message is given the class name
'hidden'
You can select the icon and error message based on the input element itself :
const invalidateElement = (element) => { const currentElement = element; const errorIcon = currentElement.nextElementSibling; const errorMessage = currentElement.parentElement.nextElementSibling; currentElement.classList.add('invalid'); errorIcon.classList.remove('hidden'); errorMessage.classList.remove('hidden'); };
You can do the same for your reset element function. So you don't have to select icons and error messages by document, you can delete the previous code :
const errorMessage = document.querySelector(".error-message"); // remove this line const errorIcon = document.querySelector(".error-icon"); //remove this line
- Error icon not center align
You should remove the
margin-bottom
property on the input tag.input { display: block; width: 100%; height: 5.6rem; /* margin-bottom: 0.6rem; Remove this line */ padding-left: 2.5rem; font-family: inherit; border: 1px solid $color-grayish-blue; border-radius: 5px; }
Maybe a little suggestion, you should give the
margin-top
andmargin-bottom
properties with the value1rem
in your error message.Hopefully can help. Happy coding
Marked as helpful1@ponhuangPosted over 2 years ago@cholis04 Hello Nurcholls, how are you?
This helps a lot :D In the beginning, I was thinking do I have to use loop to make it work, but after it came out crazy complicate solution don't even know how to write and fix. But your method is much simple and easier to understand.
Thank you so much, and thanks for helping.
Have a nice day~
1
Please log in to post a comment
Log in with GitHubJoin 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