hotmonkeyac
09-28-2007, 08:56 PM
I have a script that will allow users to add links to a <div> box as a list. It's a simple script but I would like to know if its possible to store the results of the pages they add in a cookie (for future visits). Here is the code:
<script type="text/javascript">
<!--
function addEvent()
{
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById("theValue").value -1)+ 2;
numi.value = num;
var divIdName = "my"+num+"Div";
var newdiv = document.createElement('div');
newdiv.setAttribute("id",divIdName);
newdiv.innerHTML = "Added! <a href=\"javascript:;\" onclick=\"removeEvent(\'"+divIdName+"\')\">Remove</a>";
ni.appendChild(newdiv);
}
function removeEvent(divNum)
{
var d = document.getElementById('myDiv');
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);
}
//-->
</script>
Then, the following code is placed in the body:
<input type="hidden" value="0" id="theValue" />
<p><a href="javascript:;" onclick="addEvent();">Add</a></p>
<div id="myDiv"> </div>
When the user clicks "Add", it adds the page to their list. Can someone please help me store this in a cookie? Thank you.
Edit: Even if someone can point me in the direction of a simple script that stores a list of items as a cookie, I should be able to figure it out.
<script type="text/javascript">
<!--
function addEvent()
{
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById("theValue").value -1)+ 2;
numi.value = num;
var divIdName = "my"+num+"Div";
var newdiv = document.createElement('div');
newdiv.setAttribute("id",divIdName);
newdiv.innerHTML = "Added! <a href=\"javascript:;\" onclick=\"removeEvent(\'"+divIdName+"\')\">Remove</a>";
ni.appendChild(newdiv);
}
function removeEvent(divNum)
{
var d = document.getElementById('myDiv');
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);
}
//-->
</script>
Then, the following code is placed in the body:
<input type="hidden" value="0" id="theValue" />
<p><a href="javascript:;" onclick="addEvent();">Add</a></p>
<div id="myDiv"> </div>
When the user clicks "Add", it adds the page to their list. Can someone please help me store this in a cookie? Thank you.
Edit: Even if someone can point me in the direction of a simple script that stores a list of items as a cookie, I should be able to figure it out.