Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted almost 3 years ago

Tip Calculator Using HTML, CSS, Javascript and SASS

sass/scss
P
Michael Tze•480
@Biggboss7
A solution to the Tip calculator app challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


Hi Everyone, please help me with these issues :

  1. Can we use input "radio" and modify it for tip selection ? If yes, please refer me to your github page so I can learn how to do it. Currently I just use div tag that plays role as button.
  2. My reset button cannot reset the input value.

I hope you guys can help me to solve these issues. Thankyou in advance.

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Mi Vu•380
    @gerichilli
    Posted almost 3 years ago

    Hi Michael, Nice to meet you 😊,

    I hope my following answers can help you

    Q1. Yes, you can completely implement tip select with radios, and it is a best practice, much better than using div. Getting form data from a div tag will be more complicated and inefficient than getting it from a form element.

    • Step 1: We create a series of input labels (input comes first, followed by its label). All inputs must have the same name attribute. To ensure accessibility, you should wrap them in a fieldset tag. (References: fieldset) 
    <fieldset>
       <legend>Select Tip %</legend>
       <div>
          <input name="tips" type="radio" value="5" />
          <label for="tip-5"> 5% </label> 
          <input name="tips" type="radio" value="10" />
          <label for="tip-10"> 10% </label>
          <input checked="checked" name="tips" type="radio" value="15" />
          <label for="tip-15"> 15% </label> 
          <input name="tips" type="radio" value="25" /> 
          <label for="tip-25"> 25% </label> 
          <input name="tips" type="radio" value="50" /> 
          <label for="tip-50"> 50% </label> 
          <input name="tips" type="number" value="" placeholder="Custom" />
      </div>
    </fieldset>
    
    • Step 2: One thing you should avoid doing is hiding inputs with display: none.  You can hide them like this to ensure web accessibility (I copied from Tailwind CSS)
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
    
    • Step 3: Create styles for the labels. Their styles are what you wrote for the div tag (your tip select button).

    • Step 4: Since the input and the label (with the for attribute being the id of the input) are connected, when you click on a label, you will also select the radio button corresponding to that label. So when a radio is selected, its corresponding label will be added with the class selected . This can be achieved through CSS using CSS Combinators. Here I use +

    input:checked + label {
         color: hsl(183deg, 100%, 15%);
         background-color: hsl(172deg, 67%, 45%);
    }
    

    We're done with it.  Here is my solution:  Github link Live

    Q2. In response to the second question, I see that your form can be reset using the reset button, but the input fields must still be filled out. 

    I would be very grateful if you star my github repo (Since I'm in the process of looking for a new job 😉)

    Marked as helpful
  • Elaine•11,360
    @elaineleung
    Posted almost 3 years ago

    Hi Michael, just wanted to quickly answer your first question: Yes, it can be done! That's what I did in my solution, and I also made the custom input function like a radio button option so that the entire group of buttons can be seen as one group when used with the tab key. Anyway, instead of explaining how, I'll just share my solution link with you: https://www.frontendmentor.io/solutions/responsive-tip-calculator-app-with-plain-js-Nj1Gtzub_V

    It might help you with the reset button question as well. Good luck!

    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

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

Oops! 😬

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

Log in with GitHub