Ask Question
27 April, 03:58

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.

+5
Answers (1)
  1. 27 April, 05:46
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers