Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found

Submitted

[2nd] Pricing Cards w/ toggle btn, Mobile First, javascript

Yuko Horita• 645

@Sloth247

Desktop design screenshot for the Pricing component with toggle coding challenge

This is a solution for...

  • HTML
  • CSS
  • JS
2junior
View challenge

Design comparison


SolutionDesign

Solution retrospective


This is second challenge after CSS did not work for toggle button. However my JS also does not work... Any suggestions are welcome!

Community feedback

P
Grace• 27,950

@grace-snow

Posted

Hi you need to use radio inputs for that toggle, not a checkbox. It's inaccessible as it is at the moment.

It also needs focus-visible styles

The reason your js isn't working is because querySelectorAll returns an array, but you are trying to use a method for a single dom node. You need to do the classist toggle in a loop instead.

Something like this:

function togglePrices(array) {
  array.forEach(item => item.classList.toggle("hidden"));
}

slider.addEventListener("click", function () {
  togglePrices(monthly);
  togglePrices(annual);
});

Other best practices:

  • use hidden attribute instead of a hidden class if all it's doing is display none
  • target selectors using js-specific classes, such as js-monthly-price etc so there can never be conflict over styling vs javascript. Another developer working in HTML/css may change a class, not realising it is used in javascript.
  • if you do need to use a state class, try using prefixes like is. e.g. is-hidden or is-active or is-open etc
  • keep event listeners and functions they run separate

Marked as helpful

1

Yuko Horita• 645

@Sloth247

Posted

@grace-snow Hi Grace, thank you very much for your feedback as always. I will try your solution along with learning js more.

0

Please log in to post a comment

Log in with GitHub
Discord logo

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