/    Sign up×
Community /Pin to ProfileBookmark

Solve my simple problem

i m tring to read from cookie and add to cookie .My code first time runs properly but after some deletion ‘remove from cookie’ is not working.then only add to cookie is working properly.To remove from cookie you have to select one from the list.i have failed to solve.Please help me quickly

Here is my code

<html>
<head>
</head>

<script language=”JavaScript”>

var expDays =60;
var exp2 = new Date();
exp2.setTime(exp2.getTime() + (expDays*1000));

function del_c(){
var i=document.output.output_select.selectedIndex+1;
var cookVal=document.output.output_select.options[i-1].value;

var exp1 = new Date();
exp1.setTime (exp1.getTime() -1);

//var cval=GetCookie(‘cookie_item’+i);

document.cookie = ‘cookie_item’+i+ “=” +cookVal+ “; expires=” + exp1.toGMTString();

window.location = window.location;
//document.write(“name: “+cval+” index:”+i);

}

function add()
{
var NumToDoItems = GetCookie(‘cookie_list’);
//document.write(” it :”+NumToDoItems);
var i;
var ToDoItem;
if (NumToDoItems == null) {
NumToDoItems = 0;
}

//document.write(document.forms[0].elements[0].value);
var len=document.input.length;

for (var counter=0;counter < len;counter++)
{
if(document.forms[0].elements[counter].checked==true)
//if(document.enter.)
{

NumToDoItems++;
SetCookie(‘cookie_item’+NumToDoItems, document.forms[0].elements[counter].value, exp2);
SetCookie(‘cookie_list’,NumToDoItems, exp2);
window.location = window.location;
}
}

}

function SetCookie (name, value) {

var argv = SetCookie.arguments;

var argc = SetCookie.arguments.length;

var expires = (argc > 2) ? argv[2] : null;

var path = (argc > 3) ? argv[3] : null;

var domain = (argc > 4) ? argv[4] : null;

var secure = (argc > 5) ? argv[5] : false;

document.cookie = name + “=” + escape (value) +
((expires == null) ? “” : (“; expires=” + expires.toGMTString())) +
((path == null) ? “” : (“; path=” + path)) +

((domain == null) ? “” : (“; domain=” + domain)) +

((secure == true) ? “; secure” : “”);
}

function GetCookie (name) {

var arg = name + “=”;

var alen = arg.length;

var clen = document.cookie.length;

var i = 0;

while (i < clen) {

var j = i + alen;

if (document.cookie.substring(i, j) == arg)

return getCookieVal (j);

i = document.cookie.indexOf(” “, i) + 1;

if (i == 0) break;

}

return null;
}

function getCookieVal (offset) {

var endstr = document.cookie.indexOf (“;”,offset);

if (endstr == -1)

endstr = document.cookie.length;

return unescape(document.cookie.substring(offset,endstr));
}

function welcomeBack( )
{
var sCount=0;

//document.write(“hi”);
var NumToDoItems = GetCookie(‘cookie_list’);

if (NumToDoItems == null) {
NumToDoItems = 0;
}

var ToDoItem;
for (i=1; i <= NumToDoItems; i++) {
ToDoItem = GetCookie(‘cookie_item’+i);
//document.write(ToDoItem);
if (ToDoItem != null) {
document.output.output_select.options[sCount]=new Option(ToDoItem,ToDoItem,false,false);
sCount++;
}
}

}

</script>

<body>
<form name=”input” >

<input type=”checkbox” name=”name1″ value=”1″>1
<input type=”checkbox” name=”name2″ value=”2″>2
<input type=”checkbox” name=”name3″ value=”3″>3

<input type=”button” name=”Submit” value=”add” onClick=”add()”>

</form>
<form name=”output” >

<p align=”center”>
<select name=”output_select” size=”5″ >
</select>
</p>
<p align=”center”>
<input type=”button” name=”Submit2″ value=”remove” onClick=”del_c()”>
</p>
</form>

<SCRIPT LANGUAGE=”JavaScript”>
<!– Begin
welcomeBack( );
// End –>
</SCRIPT>

</body>
</html>

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @shanto spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...