Trying to get a "snapshot" of a form but am losing the values
<p>I have a highly dynamic form where interacting with a top-level element will completely transform the elements in the lower level part of the form.  I wanted an easy way to maintain state so that if the user partially entered in form data for one of the top level categories, they could switch to something else (temporarily lose all the data they typed as well as the actual input elements) and then click back to that previous top-level category and restore everything as it was.</p>

<p>I was planning something simple like, on clicking the top-level element, to do something like:</p>

<pre><code>form_state[category] = $('form').html();

category = this.val();

if (form_state[category]) {
  $('form').html(form_state[category])
}
</code></pre>

<p>However, I quickly found that the html of the form was not including the values of the form elements that the user had interacted with (it was all the original default values).  So I then realized that I need to collect all of the values of the elements within the form, and then reset them manually after resetting the form's inner html...  So I am thinking something like:</p>

<pre><code>$('input, select, textarea').each(function(el) {
  values.push($(el).val());
}
</code></pre>

<p>and then re-iterate over those and update the elements with something like:</p>

<pre><code>if (form_state[category]) {
  $('form').html(form_state[category])
  $('input, select, textarea').each(function(el, i) {
    $(el).val(values[i]);
  }
}
</code></pre>

<p>But I was just wondering if anyone has come across something like this before and knows of a better solution than manually iterating over these?  I was wondering if there is any sort of way to do a reverse form.serialize()?  That way I could just store a serialized form snapshot also and update the entire form with the serialized data.  Since .serlize() returns an escaped string, it seems like it might be more work than it's work and manually iterating over the form element types is a better way to go... ?</p>
