addButtonListenersa. Selects all buttons nested inside the main elementb. If buttons exist:c. Loop through the NodeList of buttonsd. Gets the postId from button.dataset.postIde. If a postId exists, add a click event listener to the button (referenceaddEventListener) - inside the loop so this happens to each buttonf. The listener calls an anonymous function (see cheatsheet)g. Inside the anonymous function: the function toggleComments is called with theevent and postId as parametersh. Return the button elements which were selectedi. You may want to define an empty toggleComments function for now. The listenertest will NOT pass for addButtonListeners until toggleComments is completed.Nevertheless, I recommend waiting on the logic inside the toggleCommentsfunction until we get there. const addButtonListeners = function(){ const buttons = document.querySelectorAll("main")[0].querySelectorAll(`button`); buttons.forEach((button) => { const postID = button.dataset.postId; button.addEventListener("click",function() { toggleComments(postID); }) }) return buttons; } function toggleComments(postID){ } This is what I currently have but I need the function addButtonListeners should add a click listener that calls the toggleComments function to each button element found within the main element.
addButtonListeners
a. Selects all buttons nested inside the main element
b. If buttons exist:
c. Loop through the NodeList of buttons
d. Gets the postId from button.dataset.postId
e. If a postId exists, add a click event listener to the button (reference
addEventListener) - inside the loop so this happens to each button
f. The listener calls an anonymous function (see cheatsheet)
g. Inside the anonymous function: the function toggleComments is called with the
event and postId as parameters
h. Return the button elements which were selected
i. You may want to define an empty toggleComments function for now. The listener
test will NOT pass for addButtonListeners until toggleComments is completed.
Nevertheless, I recommend waiting on the logic inside the toggleComments
function until we get there.
const addButtonListeners = function()
{
const buttons = document.querySelectorAll("main")[0].querySelectorAll(`button`);
buttons.forEach((button) =>
{
const postID = button.dataset.postId;
button.addEventListener("click",function()
{
toggleComments(postID);
})
})
return buttons;
}
function toggleComments(postID)
{
}
This is what I currently have but I need the function addButtonListeners should add a click listener that calls the toggleComments function to each button element found within the main element.
Unlock instant AI solutions
Tap the button
to generate a solution