Design comparison
Solution retrospective
Hello!
Very exciting stuff, as this is my first time using what I have learnt about JavaScript to build a dynamic page!
I'm sure there are countless ways to achieve the same effect, what do you think of the JavaScript I used to get the desired effect?
Community feedback
- @fazzaamiarsoPosted over 2 years ago
Hello Anubliss! Nice work!
I think your implementation is valid, but can be more efficient. Here is my refactor.
buttons.addEventListener("click", setScore) function setScore(e) { //using event const clickedElement = e.target; if(clickedElement.tagName !== "BUTTON") return; // don't do anything if the clicked element is not button score = parseInt(clickedButton.value); buttons.forEach((button) => { button.classList.remove(`orange`); }); clickedElement.classList.add(`orange`); }
There I use,
event
that is passed by the addEventListener. Event will give you some useful information like what element triggered the event (target), where the event is attached (currentTarget). https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener.I hope it's useful! Cheers!
Marked as helpful1
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