Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through cookie values?!?!?

Status
Not open for further replies.

syy

Programmer
Joined
Feb 28, 2006
Messages
2
Location
GB
Hi All

I have a bit of a problem, i am creating a cookie and adding content not a problem, but when i try and retrieve the content i am having the problem,

basically i have the following values in my cookie

sections:3&region1:London&nation1:England&region2:Manchester&nation2:England&region3:Glasgow&nation3 :Scotland

the regions and nations could be empty and could go up to 10 or more i.e region10: and nation10:
the sections value will always relate to how many i have - so in this example there are 3

so what i was trying to do was a simple for loop and add the value onto the end like this

for(i=0;i<sections;i++){
alert(cookie.region);
alert(cookie.nation);
}

but it tells me
Error: cookie.region has no properties

Can anyone help me loop through this?
 
Try this:

Code:
for(var i=0; i<sections; i++){
	alert(cookie['region' + i]);
	alert(cookie['nation' + i]);
}

I've not tried it myself, but given that "region" and "nation" are neither properties nor arrays, it can't be any more broken than what you have right now ;-)

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Dan your a star it worked!

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top