I couldn't figure out how to prevent user from submitting without having rating selected, but I didn't know how to check if rating button is not clicked and display an alert message. Tried also to check if :focus on any button is active and if not, then display an alert, but I couldn't reach it in JS. My next idea was to change buttons into radio inputs, hide them and catch their labels as buttons, but I think a label can't have an active state like :focus so I gave up. Any ideas how to solve the problem? Also any other feedback and advice would be highly appreciated.
Fahad Akram
@FaDiiiLeoAll comments
- @kacper-reimanSubmitted almost 2 years ago@FaDiiiLeoPosted almost 2 years ago
Hey Kacper, you have done a good work!
You can use following tips regarding your question about preventing user from submitting if a rating is not selected:
Instead of using buttons for ratings, you can use <input type="button" > and use value attribute to show rating numbers and also to store selected rating in a variable in javascript. You will need to add click event listeners on all rating buttons and store their value in variable using e.target.value. You can initialize the variable with 0 and when user submits the rating, do a simple if else and check if variable is equal to 0 (means no rating is selected) show an alert message, else you can show your thank you card with the selected rating. Hope this helps and if you have any question or need further help you can ask here.
Marked as helpful0