Ask Question
29 April, 16:52

Create the setupcart () function. The purpose of this function is to define the event handlers for the Add to Order buttons on the page. Add the following commands to the function: a. Create a variable named addbuttons, which contains the collection of elements belonging to the addButton class. b. Loop through the addButtons collection and for each button, add an event handler that runs the addItem () function when the button is clicked.

+2
Answers (1)
  1. 29 April, 17:13
    0
    window. addEventListener ("load", setupCart);

    function setupCart () {

    var addButtons = document. querySelectorAll ("addButton");

    for (var i = 0; i < addButtons. length; i++) {

    addButtons[i]. o nclick = addItem ();

    }

    }

    function addItem (e) {

    var foodItem = e. target. nextElementSibling;

    var foodID = foodItem. getAttribute ("id"). value;

    var foodDescription = foodItem. cloneNode (true);

    var cartBox = document. getElementById ("cart");

    var duplicateOrder = false;

    / / Add duplicate items to cartBox

    for (var i = 0; i < cartBox. childNodes. length; i++) {

    if (cartBox. childNodes[i]. id = = = foodID. id) {

    cartBox. firstElementChild. value + 1;

    duplicateOrder = true;

    break; / /stop processing the for loop

    }

    }

    / / Add new items to cartBox

    if (! duplicateOrder) {

    var orderCount = document. createElement ("span");

    orderCount. textContent = 1;

    foodDescription. appendChild (orderCount);

    cartBox. appendChild (foodDescription);

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Create the setupcart () function. The purpose of this function is to define the event handlers for the Add to Order buttons on the page. ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers