Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted about 2 years ago

Interactive Rating Component (HTML CSS Javascript)

Soraya•230
@Sorpanda
A solution to the Interactive rating component challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

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?

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • dia ♡•200
    @diaasaur
    Posted about 2 years 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:

    1. You have two articles, survey and afterSubmit, which are initially arranged one after another in the DOM/HTML.
    2. Initially, to hide the afterSubmit element, you added the CSS rule display: none.
    3. You used an onclick event handler function called switchVisible to make survey invisible and afterSubmit visibile. This function performs two actions: a. It changes the display property of the survey from its default value display: block to display: none, effectively hiding it. b. It changes the display property of the afterSubmit element from display: none to display: 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 because survey IS visible if you are able to see the survey and the submit button

    to (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 using document.getElementById('myElement').classList.add('myClass') or document.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 helpful

Join 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
Frontend Mentor logo

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit all CSS, SCSS and Less files in your repository.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.

How does the JavaScript validation report work?

When a solution is submitted, we use eslint to run an automated check on the JavaScript code.

The report picks out common JavaScript issues such as not using semicolons and using var instead of let or const, among others.

The report will audit all JS and JSX files in your repository. We currently do not support Typescript or other frontend frameworks.

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub