PDA

View Full Version : Displaying multiple, identical cookies?



adamdressler
12-10-2011, 09:13 AM
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.





<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>

Old Pedant
12-10-2011, 09:17 AM
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.