JavaScript: preserve fields after refresh

Posted on 2006-05-16
Last Modified: 2012-05-05
below is a javascript function which dynamically appends some fields to a form.
Using a submit button:
<input type = "submit" value = "More on this date" name = "more" onClick="AddText();">

i want to preserve these added fields if the page refreshed after clicking this button. Is this possible?
I really need your help. thanks.

     var fieldCount = 2;  #the first two fields
     var fieldCount1 = 2;

    function addSku() {


       var node=document.createTextNode("GEAR (Enter SKU): ");

       var newFriend = document.createElement('input');
       newFriend.type = 'text';
       newFriend.size = '17';
       newFriend.value = '';
       newFriend.onFocus = this.value=""; = 'sku' + fieldCount; = 'sku' + fieldCount;

       var node=document.createTextNode(" ");

       var newFriend1 = document.createElement('input');
       newFriend1.type = 'checkbox'; = 'gear' + fieldCount1; = 'gear' + fieldCount1;
   var node=document.createTextNode(" Review?");

var newFriend2 = document.createElement('br');

Question by:JaimeJegonia
    LVL 30

    Expert Comment

    if you are submitting on the same page, just put your function on the onload event.

    <body onload="addSku()">
    LVL 63

    Accepted Solution

    No, it is not possible. The HTTP protocol is a stateless protocol. Therefore is the browser loading into the window that what is sent for the URL. There is no state before and after the page.
    So if you want to repeat some user actions then you need some onLoad function which will look for some cookies that are not cleared on page form onSubmit action and try to analyze what you have to recreate on the page. But that is definitely not a behavior and design that I would recommend to you.
    LVL 5

    Assisted Solution


    Instead of using a submit button why dont you just use a normal button to fire the event. You could then use javascript to dynamically create the new rows without leaving the page

    LVL 41

    Assisted Solution

    As noted above, HTTP is stateless.  So, in order to have this information "preserverd", you could/should save information into cookies.
    LVL 8

    Expert Comment

    If someone fills out an form and did not add all info or something was incorrect, you want to display back to the what they typed  -  is that correct?????  

    (reload the form with original info intact?)

    Do you have PHP avail?

    Author Comment

    thanks to everyone who answered

    Author Comment

    i used a button instead, so definitely the page would not be refreshed. It has an "onclick" attribute which calls a function in javascript to retrieve the data from the fields. javascript-perl connection is done through ajax.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
    When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (, typical array handling might look like this: (CODE) B…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now