Home > DeveloperSection > Interviews > How to setting a cookie with the contents of a textbox ?
Awadhendra Tiwari

Total Post:510


Posted on    May-18-2011 9:36 AM

 JavaScript JavaScript 
 1 Answer(s)
  1144  View(s)
Rate this:

Awadhendra Tiwari

Total Post:510

Posted on    May-18-2011 12:00 AM

Values stored in cookies may not have semicolons, commas, or spaces. You should use the handy "escape()" function to encode the values, and "unescape()" to retrieve them.

//Sets cookie of current value for myTextBox
function TextBoxOnchange() {
var myBox = window.document.getElementById(myTextBox");
document.cookie = "myTextBox="+ escape(myBox.value) + getExpirationString();

//return a string like ";expires=Thu, 5 Jan 2006 16:07:52 UTC"
function getExpirationString() {
var exp = new Date();
var threemonths = exp.getTime()+(120*24*60*60*1000);
return ";expires="+exp.toGMTString();

This is called from the event handler in the HTML.

<input name="myTextBox" type="text" id="myTextBox"
onchange="javascript:TextBoxOnchange()" />

Recent Activities

ashish srivastava added new Article Article Submission Process   yesterday

Tarun Kumar added new Question What is dot notation?   2 days ago

Manoj Pandey added new Blog Android Studio Vs Eclipse   5 days ago

Don't want to miss updates? Please click the below button!