Ask Question
26 November, 14:37

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.

+3
Answers (1)
  1. 26 November, 15:16
    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?
Get 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 there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers