Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    48
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Displaying multiple, identical cookies?

    Hello all!

    Quick question regarding basic cookies.

    I have a button that runs a function that creates a cookie onclick;

    document.cookie = ("cookie1=500");

    whenever i display this later, despite the fact i've clicked the button 3 times. It only displays once.

    Is there a way I could display this AS MANY TIMES as the button was clicked.

    Any help is appreciated.



    Code:
    <script type="text/javascript">
    
    
    
    
            var cookies = document.cookie;
            var cookie_name = new Array();
            var cookie_value = new Array();
    
        if (cookies.length > 0 ) {
            var cookies = cookies.split("; ");
            for( var x = 0 ; x < cookies.length ; x++ ) {
                var cookie_parts = cookies[x].split("=");
                cookie_name[x] = cookie_parts[0];
                cookie_value[x] = cookie_parts[1];
            }
    }
    
    function writeProducts() {
    
    function removeItem() {
    	cookie_name[x] + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
    }
    
    
    	var total = 0;
    	var tempstring = "";
    	var prodTable = document.getElementById('tblProducts');
    	var prodRow;
    	var prodQuantity;
    	var prodName;
    	var prodPrice;
    
        for( var x = 0 ; x < (cookie_name.length) ; x = x + 1) {
            prodRow = document.createElement('tr');
            prodName = document.createElement('td');
            prodPrice = document.createElement('td');
    	prodCheck = document.createElement('td');
            
            prodName.innerHTML = cookie_name[x];
            prodPrice.innerHTML = "$" + cookie_value[x];
    	prodCheck.innerHTML = tempstring + '<input type="button" value="Remove from Cart" />'
    
    
           
            total = parseInt(total + parseInt(cookie_value[x]));
    
            prodRow.appendChild(prodName);
            prodRow.appendChild(prodPrice);
    	prodRow.appendChild(prodCheck);
            prodTable.appendChild(prodRow);
    	
    
    	
            }
    
    
    	document.getElementById('spnProducts').innerHTML = tempstring;
    	tempstring  = tempstring + "Total :" + " " + '$' + total;	
    	document.getElementById('spnTotal').innerHTML = tempstring;
    }
    
    
    <script>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    Yes, use *another* cookie that counts the clicks. Cookies are remembered by name, so you can never have more than one of the same name.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •