Adding to Cart

showing difference between string and number

HTML

<form id="say-form" onsubmit="return false;" action="#">
    Product A: <input type="text" id="said" value="1" size="3" autocomplete="off" />
    <input type="submit" value="Add" />
</form><br />
<br />
<div id="cart"></div>

JavaScript

// Declare VARIABLES and set them same time
var form  = document.getElementById( 'say-form' );
var input = document.getElementById( 'said' );
var cart  = document.getElementById( 'cart' );
var items = 0;

// "form" will now run a FUNCTION when user submits form
form.onsubmit = function() {
    
  items = items + parseInt(input.value);
    
  // set the HTML inside the element "cart"
  cart.innerHTML = "You have " + items;
       
};

// parseInt is a FUNCTION that changes a STRING to a NUMBER