Design comparison
Solution retrospective
Hi everyone! These are my first challenges with JS and I'm still working on this one because for some reason even listener stops working, so any feedback or suggestion is welcome. Thanks!
Community feedback
- @jesse10930Posted over 3 years ago
Hi Kristina, your project looks great so far!
As to your issue with the event listener, the problem is that the target differs depending on where the mouse clicks inside the button, so the parent to which you are adding 'active' also differs. For example, if you hover on the very edge of the button and click, the 'body' element toggles the class of 'active'. If you hover on the svg image inside the button and click, the 'container-card' element toggles the class of 'active'. And if you hover inside the button but not on the svg and click, the 'article-container' element toggles the class of 'active'. So the reason it seems like it stops working is because if you click on the button to add 'active' but then move the mouse and click on a different spot in the button, the function is adding 'active' again, but to one of the other elements. And then to get rid of the active state, you'd have to click on each of those positions to remove 'active' from each of the elements to which 'active' was added.
I think your best bet would be to use querySelector to target a specific element to which you want to toggle the 'active' class inside your function, just like you did in the 'button' variable. This way the function always changes the same element rather than being dependent on 'e.target'.
button.addEventListener("click", function(e) { document.querySelector(".container-card").classList.toggle("active"); });
Hope this helps! Happy coding :)
1@kristinalesnjakovicPosted over 3 years agoHi Jesse, thank you so much! I already made a change as you advised and it works as expected :) But even more important, it is clear for me now why my solution didn't work as excepted.
1 - @tedikoPosted over 3 years ago
Hello, Kristina! 👋 Congrats on finishing another challenge! Your solution looks very good and also responds well. In addition to jesse10930 great feedback here's my few tips:
- To fit your
.main-img
image to fill full height of container you need to removemax-height
andmax-width
from image and apply these styles:object-fit: cover; height: 100%;
. - Change the
alt
attributes for the.main-img
,.avatar
, as they don't add any extra context for screen reader users. Since your images are decorative youralt
text should be provided empty (alt="") so that they can be ignored by assistive technologies.
Good luck with that, have fun coding! 💪
0@kristinalesnjakovicPosted over 3 years agoHi @tediko! Thank you for your tips, it certainly helps so I will try to make changes as you suggested :)
0 - To fit your
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