Design comparison
Solution retrospective
This project should use API/Json-file for the data, and I use data to create divs for notification items, the question is: I can not use querySelectorAll to get that divs and change the class for change unmarked status. I wondering why
Community feedback
- @chinaza-SommiePosted over 1 year ago
Hello newman-afk, Hope you are good. I saw your question and the way i understand it, you are trying to access the classes using querySelectorAll. Inorder to do this, you would need to loop through the number of divs that have the class you are trying to access. You can use forEach loop or the for loop. Whichever one you are confortable with.
Please do reach out to me to let me know if it works. Happy coding!
1@newman-afkPosted over 1 year ago@chinaza-Sommie notifications.forEach((notification) => { const element = document.createElement("div"); element.className = "notification"; …… main.appendChild(element); } this is how I created notification items, and it's working fine, the index.html is good, but when I try : const notifications = document.querySelectorAll(".notification"); notifications.forEach((notification) => { notification.classList.remove("unmarked"); console.log(notifications); }); NodeList[] length 0 is what I got in Console, I tested other className, and those created by Js won't work.
0@chinaza-SommiePosted over 1 year ago@newman-afk try console.log(notification); because its each item is being passed as notification.
Try it out and let me know
1@newman-afkPosted over 1 year ago@chinaza-Sommie NodeList[] length 0 again, I tried get querySelectorAll("div") and only the created in html shows up
0@newman-afkPosted over 1 year ago@chinaza-Sommie I found the issue. because I use querySelectorAll outside any function, so It will run at first before notification(div) added——it's async for fake api.
0@chinaza-SommiePosted over 1 year ago@newman-afk Glad you solved it. Thanks for keeping me updated as well. Happy coding!
1
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