Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted almost 2 years ago

Notifications page

Shukhrat•250
@ShukhratKholmamatov
A solution to the Notifications page challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

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!!!

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Martinsgundi•380
    @Martinsgundi
    Posted almost 2 years 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 the forEach method.

    It goes like this:

    unreadNotifications.forEach(function (unreadNotification) {
        unreadNotification.style.background = "#fff";
    });
    

    Basically, the forEachmethod 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 helpful

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
Frontend Mentor logo

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit all CSS, SCSS and Less files in your repository.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.

How does the JavaScript validation report work?

When a solution is submitted, we use eslint to run an automated check on the JavaScript code.

The report picks out common JavaScript issues such as not using semicolons and using var instead of let or const, among others.

The report will audit all JS and JSX files in your repository. We currently do not support Typescript or other frontend frameworks.

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub