Interactive Card layout - Responsive, HTML, raw CSS and Javascript
Design comparison
Solution retrospective
Per usual when I post a solution on here, I am open to all feedback and tips! Questions: Curious about my use of certain elements in the form, if they are used the right way?
I didn't have anywhere to send the form, so I just made the button type on the submit button to ''Button" so it wouldn't submit and break the page. I added an event listener to the button to toggle a class that hid the rating component and the results component.
I also didn't know how to get the "You selected X out of 5" to show the actual number that I chose on the rating component. Any advice on that would be awesome as well!
Community feedback
- @rileydevdznPosted over 1 year ago
Hi Scott,
Well done! Great job with using the type=button on your form (you're spot on with that, we only need a type=submit if there's a server somewhere to send the data to) and toggling the classes 💯
For the selectable ratings, a button element works, as you've done. Another type of input that is a bit more accurate meaning-wise (since we're providing a list of options and a user can only select 1 option) is a set of radio buttons. It takes some styling for the visual overlay of the label over the input, but the ability to select a radio button by selecting its label works to our advantage here.
I also suggest the radio buttons because there's another property of this input that feeds into your question about populating the "you selected X out of 5". We can set the
value
attribute of each radio button to represent the selected value (the number rating, just like you've done with your buttons), then iterate through the radio group with JavaScript to identify which button is checked, store the checked value as a variable and use that to dynamically update your <p> element with each new selection. In a form, only the radio button that's checked will be submitted, so it cuts down on the looping/checking you need to do.You've already got the iterating through a loop handled but here's an example that loops through a radio group to see which input is checked so you can see it applied to the radio group. They use a for...of loop, and I'll stick to that with my example, but don't feel like you're locked into doing it that way.
To update the "you selected X", you can create a variable from your #ratingSelected element (let's call this variable
confirmRating
). To store the checked value, let's create a variable calledyourRating
. Then update the.innerText
property ofconfirmRating
with the value of the selected input (yourRating
). So you're dynamically updating the content of the<p id="ratingSelected">
element withyourRating
. Altogether, it'd look this:const confirmRating = document.getElementById('ratingSelected'); function findChecked() { let yourRating; for (const radioButton of radioButtons) { if (radioButton.checked) { yourRating = radioButton.value; break; } confirmRating.innerText = `You selected ${yourRating} out of 5`; }
I put it inside a function so we can call the function with our event listener, like the class toggling you've already got 😊
Great job incorporating global variables on the root and making sure your images have alt text!! 🙌 I just noticed you've got two
<h1>
elements, go ahead and make the heading in the thank you card an<h2>
since we should only have one h1 per page.Hopefully that helps and happy coding!
Marked as helpful0
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