Make a selected/clicked button active in JavaScript without Loop
Design comparison
Community feedback
- @correlucasPosted about 2 years ago
👾Hello @Darionas, Congratulations on completing this challenge!
Great code and great solution! I’ve few suggestions for you that you can consider adding to your code:
Your component is perfect, but is not responsive yet, this is due the
fixed width
you've applied to the container.Look both
width
andmax-width
the main difference between these properties is that the first(width) is fixed and the second(max-width) isflexible
, for example, a component withwidth: 320px
will not grow or shrink because the size will be ever the same, but a container withmax-width: 320px
ormin-width: 320px
can grow or contract depending of the property you've set for the container. So if you want a responsive block element, never usewidth
choose ormin-width
ormax-width
.✌️ I hope this helps you and happy coding!
Marked as helpful0@DarionasPosted about 2 years ago@correlucas Thanks for quick response. I very appreciate it.
0 - @edo979Posted about 2 years ago
Hi.
You done great job, well done. I read you code and shorten your function.
let num; let prevButton; const numCont = document.getElementById('numCont'); function myId(clicked_id) { num = document.getElementById(clicked_id).value; numCont.addEventListener('click', (e) => { const isButton = e.target.nodeName === 'BUTTON'; if (!isButton) return; prevButton?.classList.remove('active'); if (isButton) { e.target.classList.add('active'); } prevButton = e.target; }) }
Of course you must add defer in your script tag. Read this:
Marked as helpful0
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