Hi,

I have a site with a few pages and different forms on each page (like lots of sites.) If a user visits a page, modifies a value in one of the form fields and then clicks away to another page without hitting save I would like to throw up an alert asking if they would like to save before changing pages.

To do this I'm guessing I need to loop through the form values when the page loads, store the values in some sort of structure (like an array or object) and do the same when it unloads (window.onunload), and then compare the two to see if any changes were made.

Alas, I'm not sure how to do this. I know how to loop through a form. Just do something like:
Code:
var theForm = document.forms[0];
for(i=0; i<theForm.elements.length; i++) {
}
One way I tried is to make an object like:
Code:
function formSnapshot() {

}
and then when the page loads create a new instance, and add properties using the prototype, like this:
Code:

function cacheFormValues() {

  var snapshot = new formSnapshot();

  var theForm = document.forms[0];
  var propertyName = new Array();
  for(i=0; i<theForm.elements.length; i++) {

    propertyName[i] = theForm.elements[i].name;
    snapshot.prototype.propertyName[i] = theForm.elements[i].value;

  }
}
The problem is, JS won't allow me to use the line
snapshot.prototype.propertyName[i] = theForm.elements[i].value;

Does anyone have any ideas about how I may solve this problem, or about how to add dynamic property names to an object using object.prototype.dynamic_name ?

t {H} a n K s