Design comparison
Solution retrospective
Got trouble on js code-->couldn't use one class for three unread notification to change in js, so I made 3 id.
Feel free to give feedback!!!
Community feedback
- @MartinsgundiPosted about 1 year ago
Hello Shukhrat, nice work!
Regarding your issue with JavaScript, using the same class name to select multiple elements is very common in web development.
There is a method in JavaScript that allows you to get multiple elements with the same class name or tag name. It’s done by simply typing
document.querySelectorAll(‘.classname’)
.querySelectorAll
stores all the elements that contains the specified class name into a NodeList, which is very similar to an array.- An array in programming is like a container that allows you to store multiple pieces of data, such as numbers, words, or other information, in a structured way. The difference is that NodeLists are specifically designed for representing collections of nodes in the DOM.
By using this method, it becomes easier to target multiple elements for styling. This can be achieved by simply iterating or looping through the NodeList.
Check out MDN for more detailed information on JavaScript DOM.
Also, check out this article on freeCodeCamp on various ways to iterate or loop through an array.
Now, regarding your issue, you should give the three unread notifications the same class name that's different from the unique class name given to each individual element. In your case, all three elements share the same CSS style, but to prevent unwanted and unforeseen problems, it's preferable to give them a classname different from their individual unique class names. For example:
<div class="user_notification unread" id="unread"> <img src="assets/images/avatar-mark-webber.webp" alt=""> <!—Your remaining code goes here—> </div> <div class="user_notification unread" id="unread1"> <img src="assets/images/avatar-angela-gray.webp" alt=""> <!—Your remaining code goes here—> </div> <div class="user_notification unread" id="unread2"> <img src="assets/images/avatar-jacob-thompson.webp" alt=""> <!—Your remaining code goes here—> </div>
If you notice, I gave the three unread notifications the same class name of “unread.”
Now, in your JavaScript, you will use the
querySelectorAll
method to target or select all elements with the classname of “unread,” which in your case are the three unread notifications. Example:let unreadNotifications = document.querySelectorAll('.unread');
To add the CSS style, which is
background: #fff
to the three elements, you will need to iterate or loop through the NodeList that contains the three elements to give them the background style. There are several ways to loop through the NodeList or array, but for this explanation, I will use theforEach
method.It goes like this:
unreadNotifications.forEach(function (unreadNotification) { unreadNotification.style.background = "#fff"; });
Basically, the
forEach
method in JavaScript is used to iterate over elements in an array (or NodeList) and perform a specified action or function on each element. It takes a callback function as its argument, which is executed for each item in the array (or NodeList).After all this, your code should look something like this:
// Select all the unread notifications let unreadNotifications = document.querySelectorAll('.unread'); function readAll() { // Iterate through the NodeList and apply the background style to each of the elements in the NodeList unreadNotifications.forEach(function (unreadNotification) { unreadNotification.style.background = "#fff"; }); // Your remaining code goes here };
You should also use the same approach to remove the dots from the unread notifications. I should have written the code snippet for that, but I feel this reply is getting too long. Fortunately, they both share the same concept.
I hope this helps. Happy coding!
Marked as helpful1@ShukhratKholmamatovPosted about 1 year ago@Martinsgundi Just awesome thanks for your really helpful feedback
0@MartinsgundiPosted about 1 year ago@ShukhratKholmamatov You’re welcome🤝🏾 and I’m really glad I could help.
0
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