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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education