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.

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter13: File Input And Output
Section: Chapter Questions
Problem 6RQ
icon
Related questions
Question

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.

AI-Generated Solution
AI-generated content may present inaccurate or offensive content that does not represent bartleby’s views.
steps

Unlock instant AI solutions

Tap the button
to generate a solution

Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:
9780357392676
Author:
FREUND, Steven
Publisher:
CENGAGE L