Javascript loop input element, keeping values after back button

Posted on 2007-10-09
Last Modified: 2012-06-22
I am using a form similar to this and it works great. I am trying to repleat form elements to save on network traffic, but, once submitted and then the back button is pressed on the browser, all the values are cleared unlike if I were to have a static 10 element form (all the values would match). Is it possible to make this form work with the back button filling in the entered data?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 strict//EN" "">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Submit form</title>
  function genForm() {
        var html = "";
        var target = document.getElementById('formFields');

        for (var n=1; n <= 10; n++){
            html += '<input type="text" name="address' + n + '" value="" >';
        target.innerHTML = html;
<body onload="genForm()">
      <form action="index.html" method="post" name="myform">
              <div id="formFields"></div>
            <input type="hidden" name="state" value="ca">
            <input type="submit" value="GO">
Question by:Andrew Maurer
    1 Comment
    LVL 23

    Accepted Solution

    You could store all of the answers in a cookie and then read them when the page loads and generate your dynamic fields accordingly.  This is the only way it could be done client side.

    If I was doing this I would do it with server side code.

    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. …
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    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…

    731 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

    18 Experts available now in Live!

    Get 1:1 Help Now