Code: Select all
// Function to show div
function SCdiv(divid){
if(document.getElementById(divid).style.display == 'none')
document.getElementById(divid).style.display = 'block';
} Code: Select all
<script type="text/javascript">
window.onload=CheckCookies;
</script>
<form name="SC" method="post" action="#">
<input type="checkbox" name="test" id="test" value="Other" onclick="SCdiv('Other');" />
<div id="Other" style="display:none">
<input type="text" name="other"/>
</div>Code: Select all
//This Function Creates your Cookie for you just pass in the Cookie Name, Value, and number of days before you want it to expire.
function CreateCookie(name,value,days)
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
}
//This Function reads the value of a given cookie for you. Just pass in the cookie name and it will return the value.
function ReadCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
//This Function Erases Cookies. Just pass in the name of the cookies you want erased.
function EraseCookie(name)
{
CreateCookie(name,"",-1);
}
//Sets or UnSets Cookies for given checkbox after it's been clicked on/off.
function ChangeBox(CheckBox)
{
if (document.getElementById(CheckBox).checked)
{
var CurrentCookie = ReadCookie("Scookie");
CurrentCookie = CurrentCookie + CheckBox;
CreateCookie("Scookie",CurrentCookie,"100");
}
else
{
var CurrentCookie = ReadCookie("Scookie");
CurrentCookie = CurrentCookie.replace(CheckBox,"");
CreateCookie("Scookie",CurrentCookie,"100");
}
}
//Runs on body load to check history of checkboxes on the page.
function CheckCookies()
{
var CurrentCookie = ReadCookie("Scookie");
for (i=0; i<document.SC.elements.length; i++)
{
if (document.SC.elements[i].type == "checkbox")
{
document.SC.elements[i].onclick = function() {ChangeBox(this.id);};
if (CurrentCookie && CurrentCookie.indexOf(document.SC.elements[i].id) > -1)
{
document.SC.elements[i].checked = true;
}
}
}
}
//Clears Form
function ClearBoxes()
{
for (i=0; i<document.SC.elements.length; i++)
{
if (document.SC.elements[i].type == "checkbox")
{
document.SC.elements[i].checked = false;
ChangeBox(document.SC.elements[i].id);
}
}
}