View Full Version : easiest way to check if a form has changed
crazygamer
12-01-2005, 04:55 PM
Does anyone know what the easiest way is to check if the user has changed any of the values in a form?
I need this to provide the user with a pop-up asking to save when they leave the page, but only if they've actually changed something as I don't want to bother them with it every time.
Thanks
ecnarongi
12-01-2005, 05:12 PM
When you say changed something do you mean, populate the fields in the form? If so you can store the original value in a hidden element use onchange or onfocus to trigger that a form element has been clicked and then check to make sure that element has a value. If the value is different then ask to save.
If this is not what you are looking for please clarify.
crazygamer
12-01-2005, 05:16 PM
Yes, I mean when any value of a text input is changed or a checkbox is checked, drop-down menu item changed, etc. I understand your way, but I think there has to be something easier to do (I have over 200 I need to track).
ralph l mayo
12-01-2005, 05:17 PM
<form onchange="javascript:changed = true;">
<!-- inputs -->
</form>
also set var changed to false in an onload or something.
artman 186
12-01-2005, 05:24 PM
Is that valid? I checked the spec and I think onchange is only valid for input type='text', select boxes and textareas.
If it works, I say go for it but onchange on a form is definitely not in the spec. You may have to go the long route, and wire up onchange events to all your inputs and have them set a variable (same principle as the proposed form's onchange method). You could probably do this in your onload method, simply loop over the form's inputs and wire up the onchange event dynamically rather than have to write it into every single one. Problem is, if you have radio buttons you might have to wire up something by hand (IE, initialize the value before hand and check for it changing after submittal).
ralph l mayo
12-01-2005, 06:04 PM
Yeah, sorry, it works in ff and opera but not IE.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.