John John - 1 year ago 69
HTML Question

How can I remove all items from my array and display it?

I have a mock webstore I'm making for a project and I can't seem to get my empty cart button working. The user clicks add or remove item to add or remove items from the cart, but my button to empty the cart isn't working.

<button type="button" onclick="removeAll()" style="color:black">Empty Cart</button>

I've tried removing each item individually with one button click but it only removed the first item in the array.

function removeItem(itemIndex) { //remove item from cart

function removeAll(itemIndex) { //removes all items from cart

function Cart (holder, items) {
this.holder = holder;
this.items = items;
this.quantities = Array();
for (var i=0; i<items.length; i++)
this.quantities[i] = 0;

this.add = function (index) {
this.remove = function (index) {
if (this.quantities[index] > 0)

Any help would be appreciated, thanks

Answer Source

Looking at that code, you want removeAll to be:

function removeAll() { //removes all items from cart
    var n;
    for (n = 0; n < cart.quantities.length; ++n) {
        cart.quantities[n] = 0;

...though it would probably make sense to put removeAll on Cart.prototype (adjusted accordingly).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download