Interactive comments using CSS, a bit of bootstrap and Javascript/jque
Design comparison
Solution retrospective
Hey All, Tried my best to do this with just vanilla javascript. I realize it's not perfect, a few things I couldnt work out are:
- when adding a new comment, how to add the upvote and downvote well. I could do it but as soon as I added a new comment the variable changed and so the voting would only work on the new comment. Is there a better way to do this than adding a new script?
- a few other things like the ordering of replies break when more than one comment is added.
Any feedback would be much appreciated!
Community feedback
- @adram3l3chPosted almost 3 years ago
<div class="counter"> <button class="plus">+</button> <h2 class="value">0</h2> <button class="minus">-</button> </div> <div class="counter"> <button class="plus">+</button> <h2 class="value">0</h2> <button class="minus">-</button> </div> document.querySelectorAll(".plus").forEach((item) => { item.addEventListener("click", () => { const valueElement = item.parentNode.querySelector(".value"); valueElement.textContent = +valueElement.textContent + 1; }); }); document.querySelectorAll(".minus").forEach((item) => { item.addEventListener("click", () => { const valueElement = item.parentNode.querySelector(".value"); valueElement.textContent = +valueElement.textContent - 1; }); });
can't you just update the upvote like this. add event listener on each plus and minus button. when event is occurred select its parent and increase values inside it
Marked as helpful0@WashemjPosted almost 3 years ago@adram3l3ch Thanks for the reply! Yeh I guess you could, though doesnt the second const 'valueElement' not work cause it can only be defined once?
1@adram3l3chPosted almost 3 years ago@Washemj Nop. Those are in different scopes means different functions. The first const valueElement can't be accessed from the second callback
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