Multi-step-form | HTML | SCSS | vanilla JS
Design comparison
Solution retrospective
It was kinda hard for me to build this. I never build a multi step form before. I think my JS is a bit too long and complicated. Any tips are welcome.
Community feedback
- @Victor-NyagudiPosted almost 2 years ago
Hi, Lucas.
Good attempt on this one.
I noticed you repeated this snippet of code in your JavaScript where you check if a value is an empty string and then change its
display
property.if (nameInput.value == "") { nameInput.style.borderColor = "hsl(354, 84%, 57%)"; errorMessages[0].style.display = "block"; } else { nameInput.style.borderColor = "hsl(229, 24%, 87%)"; errorMessages[0].style.display = "none"; }
This works now, but it's fragile and can break easily.
Imagine you had to change something in this code snippet e.g. use
backgroundColor
instead ofborderColor
, you'd have to go through everyif
statement and make the change manually.You could encapsulate it in a method and call the method multiple times and supply different arguments. This way, if you need to change something, you can do so in one place.
Here's an example of the code in a method that's called multiple times.
function changeBorderColor(input, hsl) { if(input.value == ""){ input.style.borderColor = hsl; errorMessages[0].style.display = "block"; } else{ input.style.borderColor = hsl; errorMessages[0].style.display = "none"; } } changeBorderColor(nameInput, "hsl(229, 24%, 87%)"); changeBorderColor(mailInput, "hsl(229, 24%, 87%)"); changeBorderColor(numberInput, "hsl(354, 84%, 57%)");
Which of these looks cleaner and easier to maintain to you?
I'll let you decide.
I hope this helps.
All the best with future solutions.
1@llKryptonixllPosted almost 2 years ago@Victor-Nyagudi thanks for the help, I will use this way, it looks much cleaner
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