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!

Removing a number from a text box and putting it in a cookie

Status
Not open for further replies.

Justin2k

Programmer
Joined
Jul 28, 2004
Messages
4
Location
GB
Hi, what l want to do is once a user has changed an amount (which will be a number between 1 & 99) in a text box, l want a cookie, which contains a list of information about various items, to be updated. i.e to change one items amount to the value the user just placed in the text box.

The cookie looks like this: cost=20.00/Smoking/1::cost=25.00/Slips And Trips/4::cost=25.00/Substance/2::

Where cost is the name of the cookie and each item is separated by '::'. The last digit in each item is the one l want to update.Here is a code segement:

// 'for loop' splits each item in thisCookie array at the '=' sign, this takes out the cookie name from the string (i.e 'cost')
for(j=0; j<thisCookie.length-1; j++){
thisCookies = thisCookie[j].split("=");

// 'for loop' splits each item in 'thisCookies' array at each '/' and then places each part in a table
for (i=1; i<thisCookies.length; i++){
pieces = thisCookies.split("/");
document.write('<tr><td width="335">');
document.write(pieces[1]);
document.write('</td><td width="95">');
document.write('<input name="amount'+j+'" type="text" id="amount_'+j+'" value="'+pieces[2]+'" onblur="ChangeNumber('+pieces[1]+',?????);" size="2" maxlength="2" />');
document.write('</td><td width="78">');
document.write("£");
document.write(pieces[0]);
document.write('</td></tr>');

total += parseFloat(pieces[0]);
}
}

Not sure how to get the value of the text box into the '?????' bit though. Here is the code for the function that is called:

function ChangeNumber(strObjId, nNb){
MyArrayOfItem = document.cookie.split("::");
for(i=0;i<MyArrayOfItem.length-1;i++){
MyItemStuff = MyArrayOfItem.split("/")
if(MyItemStuff[1]== strObjId){
var MyNewItemStuff = MyItemStuff[0]+"/"+MyItemStuff[1]+"/"+nNb;
MyArrayOfItem=MyNewItemStuff;
}
}

var ret = "";
for(i=0;i<MyArrayOfItem.length-1;i++){
if(ret == "") ret = MyArrayOfItem;
else ret += "::" + MyArrayOfItem;
}

MyCookie = ret+"::";
document.cookie = MyCookie;
alert(MyCookie);
}

What l need is to get 'nNb' to equal the value of the text box, which means that the '?????' has to equal the value of the text box. Any suggestions? ;)
 

If you replace "?????" with "this.value", that should give you the value of the text box.

Hope this helps,
Dan
 
Thanks Dan, unfortunately that doesn't seemed to have helped. In I.E nothing happens when l change the value in the text box. (i.e the cookie is not updated). And in Netscape l get the error:

Error: missing ) after argument list
Source Code:
ChangeNumber(Substance Abuse,this.value);

This shows that the first element is correct but second, this.value, is not changing to the correct value!?! I know what this error usually means but there doesn't seem to be a problem with my code! l have no idea what the problem is. More help needed please :)

I changed that line to:
Code:
document.write('<input name="amount'+j+'" type="text" id="amount_'+j+'" value="'+pieces[2]+'" onblur="ChangeNumber('+pieces[1]+',this.value);" size="2" maxlength="2"/>');
 
Ok, l've managed to get the function to be called but only when l specify the first element of 'onblur' (e.g. Smoking).

Code:
document.write('<input name="amount'+j+'" type="text" id="amount_'+j+'" value="'+pieces[2]+'" onblur="ChangeNumber(\'Smoking\',this.value);" size="2" maxlength="2"/>');

What l want is that the 'Smoking' value to be the value of 'pieces[1]'.
 
Thanks Dan, got it sorted now. Your answer is nearly correct. This is what l've used to get it to work:

Code:
onblur="ChangeNumber(\' '+pieces[1]+'\',this.value);"

Needed to put it in quotes.
&:-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top