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

Tip Calculator

Trey•290
@willetto
A solution to the Tip calculator app challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


Please no copy/paste comments. :)

The main area I struggled in was with the tip buttons. It all works. But is there a better way to deal with both assigning values and giving them an "active" color?

HTML Buttons all looked like this:

<button
            type="button"
            class="tip-percent-input"
            onClick="reply_click(this.value)"
            value="0.05"
          >  
            5%
</button>

These are the 2 main JS sections for the buttons

//Assigning the button value
function reply_click(clicked_id) {
  tipPercent = Number(clicked_id);
  calcTip();
}

//Assigning the button color on click
tipButtons.forEach(input =>
  input.addEventListener('click', function () {
    for (const e of tipButtons) {
      e.classList.remove('btn-selected');
    }
    input.classList.add('btn-selected');
  })
);
Code
Loading...

Please log in to post a comment

Log in with GitHub

Community feedback

No feedback yet. Be the first to give feedback on Trey's solution.

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
  • Use cases

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