Tip Calculator App using HTML, CSS, Flexbox, and JavaScript
Design comparison
Solution retrospective
Hi!
I'd love any feedback on my HTML, CSS, or JavaScript. I really struggled with the styling of the :checked
state for the % buttons and after a lot of research and help from slack channel I switched from buttons
to radio inputs
. I feel pretty good about it now, except I can't seem to figure out how to remove the :checked
state on the tip inputs once the user clicks on the custom tip percent input area. I tried a few versions of this:
customTip.addEventListener("click", function () {
document
.querySelectorAll(".percent-input .percent-input.label")
.classList.remove("checked");
});
Any suggestions would be great! Thanks so much!!! Allyson
Community feedback
- @turtlecrabPosted over 2 years ago
Hey great job, it looks really close to the design!
Addressing your question - there are several inaccuracies in your code:
- your query selector parameter string looks like you are up to grab labels for radio inputs(correct query for this case would be
querySelectorAll('.percent-input + label')
), but you actually don't need labels, you need to uncheck the inputs themself! So query request here should be simplyquerySelectorAll('.percent-input')
querySelectorAll
returns a list of nodes, so you can't just use methods like.classList.remove
on it, you need to iterate over the list and do all the stuff in that loopchecked
is not a class, it's a property of an input, so to remove it we just set it tofalse
So the working solution I've come up with is:
customTip.addEventListener("click", function () { const radioInputs = document.querySelectorAll(".percent-input") radioInputs.forEach(input => input.checked = false) });
I hope that helped!
Marked as helpful1@allyson-s-codePosted over 2 years ago@turtlecrab Thanks so much!!! This was so helpful. At one point I tried
querySelectorAll('.percent-input')
but I did not understand that it returned a list of nodes and the need for the loop. And thatchecked
acts as a property of input.Thanks so much for taking the time to provide feedback. I really appreciate it!!!
Allyson
0@turtlecrabPosted over 2 years ago@allyson-s-code It's also worth mentioning that
querySelectorAll
returns not an array but a specialNodeList
object. Which conveniently hasforEach
method just like an array, but it doesn't have methods likemap
,reduce
orfilter
so if we want to use these we need to convert it to an array viaArray.from(nodeList)
or spread[...nodeList]
I'm really glad my comment did help! I actually just recently learned these dom and query related details, before that I was as confused as you were, so I'm happy to share the knowledge! Also they say the best way to learn something is to teach it :)
Marked as helpful0@allyson-s-codePosted over 2 years ago@turtlecrab thanks so much! I'll try to spread the knowledge as well! :)
0 - your query selector parameter string looks like you are up to grab labels for radio inputs(correct query for this case would be
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