Design comparison
Solution retrospective
This is my first Javascript project. I watched some courses for Javascript, learning the basics. I haven't learned all of it yet, but I felt lost so I wanted to just do a challenge without having gone through all the material and see where it brought me... I figured out how to get the rating buttons to work and show the value in the text. There might be a better/simpler way, but this was done completely on my own so I left it this way.
The showing and hiding of the 2 different elements (Survey and Thank you) after submitting was difficult. I searched the internet and found this on stack overflow, I did change the id names to apply to my code:
/*
if (document.getElementById('survey')) {
if (document.getElementById('survey').style.display == 'none') { document.getElementById('survey').style.display = 'block'; document.getElementById('afterSubmit').style.display = 'none'; } else { document.getElementById('survey').style.display = 'none'; document.getElementById('afterSubmit').style.display = 'block'; } } */
It worked but I didn't fully understand it. After reading it over and over I think it means:
if (document.getElementById('survey')) <-- this equals to true, so it runs the next if/else statement within its block. Does this if statement only run once when page is loaded? not after that?
if (document.getElementById('survey').style.display == 'none') <-- this if statement equals to false, skipping the code in the if statement and going to else instead. The else conditions then only activate when clicking on the button.
This code is supposed to be a toggle, if I would have had another button on the afterSubmit element I could click on that button and after clicking the button. The if statement: if (document.getElementById('survey').style.display == 'none') now equals to true and runs the code within, hiding the afterSubmit element and showing the survey again.
Because I didn't need to toggle between the 2 elements I simplified my code, and it works!
Please let me now if what I'm saying is wrong, and also what are potential other ways to approach this?
Community feedback
- @diaasaurPosted over 1 year ago
Hi Soraya! Great job on your first JS challenge <3
The showing and hiding of the 2 different elements (Survey and Thank you) after submitting was difficult
The task of showing and hiding the two elements (Survey and Thank you) after submitting can be challenging. Let's break down what you did to achieve this:
- You have two articles,
survey
andafterSubmit
, which are initially arranged one after another in the DOM/HTML. - Initially, to hide the
afterSubmit
element, you added the CSS rule display: none. - You used an onclick event handler function called
switchVisible
to makesurvey
invisible andafterSubmit
visibile. This function performs two actions: a. It changes the display property of thesurvey
from its default valuedisplay: block
todisplay: none
, effectively hiding it. b. It changes the display property of theafterSubmit
element fromdisplay: none
todisplay: block
, making it visible.
you can safely change switchVisible from:
function switchVisible() { if (document.getElementById('survey').style.display == 'none') { } else { document.getElementById('survey').style.display = 'none'; document.getElementById('afterSubmit').style.display = 'block'; } }
where
if (document.getElementById('survey').style.display == 'none') {}
is redundant becausesurvey
IS visible if you are able to see the survey and the submit buttonto (removing the redundant if statement):
function switchVisible() { document.getElementById('survey').style.display = 'none'; document.getElementById('afterSubmit').style.display = 'block'; }
if (document.getElementById('survey')) <-- this equals to true, so it runs the next if/else statement within its block.
Yes! you are on the right track!
When encountering an if statement, the condition inside the parentheses is evaluated. In this case, document.getElementById('survey') retrieves the HTML element with the ID
survey
. If the element exists, it is considered a 'truthy' value, and JavaScript coerces it to true. If the element does not exist (i.e., it is null), it is considered a 'falsy' value, and JavaScript coerces it to false.The if statement does not automatically run again after the page loads unless explicitly triggered by an event or condition.
Please let me now if what I'm saying is wrong, and also what are potential other ways to approach this?
- You can use a
.hidden
utility class and add/remove it on an element usingdocument.getElementById('myElement').classList.add('myClass')
ordocument.getElementById('myElement').classList.remove('myClass')
classList property MDN (BTW classList can help you add a .selected class on the clicked rating!)
.hidden { display: none; }
- Rather than using inline onclick handlers, you can attach event listeners to the submit button using JavaScript. This allows for more flexibility and separation of concerns.
Marked as helpful1@SorpandaPosted over 1 year agoThank you so much for your detailed reply! I thought I already tried to remove the if statement that was redundant but it didn't work, but maybe that was with the previous toggle version I found on internet, because I tried it again just now and now it does work! :) Again, thanks so much for your reply @diaasaur
1 - You have two articles,
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