Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>How about creating an <code>&lt;input type="hidden"&gt;</code> (with no <code>name</code> or outside the form so it's not submitted) in which you store an encoded list of extra fields to add and their values? While the browser won't remember newly-added fields on ‘back’, it <em>will</em> remember the values of hidden fields that were in the form from the start.</p> <p>Here's an example that saves the extra fields on document unload and retrieves them on ready:</p> <pre><code>&lt;input type="hidden" id="remembertexts" /&gt; &lt;form action="http://www.google.com/" method="get"&gt; &lt;div id="texts"&gt; &lt;input type="text" name="text[]" value="" /&gt; &lt;/div&gt; &lt;div&gt; &lt;input type="submit" /&gt; &lt;input type="button" id="addtext" value="+" /&gt; &lt;/div&gt; &lt;/form&gt; &lt;script type="text/javascript"&gt; // Add new field on button press // $('#addtext').click(function() { addInput(''); }); function addInput(text) { $('#texts input').eq(0).clone().val(text).appendTo('#texts'); }; // Store dynamic values in hidden field on leaving // $(window).bind('beforeunload', function() { var vals= []; $('#texts input').each(function() { vals.push(encodeURIComponent(this.value)); }); $('#remembertexts').val(vals.join(';')); }); // Retrieve dynamic values on returning to page // $(function() { var extratexts= $('#remembertexts').val().split(';').slice(1); $.each(extratexts, function() { addInput(decodeURIComponent(this)); }); }); &lt;/script&gt; </code></pre> <p>Notes:</p> <ul> <li><p>You can use <code>form.onsubmit</code> instead of <code>window.onbeforeunload</code> if you only need it to remember values over a submission. <code>onunload</code> doesn't work as some browsers will already have stored the old form values before that event occurs.</p></li> <li><p>In Firefox the position of the hidden input is important. For some reason, if you put it below the dynamically-added fields, Firefox gets confused about which input it is and fails to remember the value.</p></li> <li><p>This example doesn't work in Opera. It can be made to work in Opera, but it's a pain. Opera's calling of load and unload events is inconsistent so you have to use onsubmit instead, or setting the hidden field on a polling interval, or something. Worse, when Opera remembers previous form-field values, it actually doesn't fill them in until <em>after</em> onload has fired! This already causes many, many form-scripting problems. You can work around that by putting a small timeout in your onload to wait until the form values have gone in if you need Opera compatibility.</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload