Forum Moderators: open

Message Too Old, No Replies

a "changes not saved" warning for form fields using JS?

         

partha

1:02 am on Jan 10, 2005 (gmt 0)

10+ Year Member



How would I make something that displays a warning whenever the value of a form field on a page is changed? I want to warn users to save their changes so they don't loose them.

Rambo Tribble

2:22 am on Jan 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



onchange is an event supported by most form elements. It is fired when an element loses focus, if the value in the element has been changed since it gained focus. You might use it to display an alert, but I'm not sure you want alerts going off every time a field is changed.

rocknbil

6:48 pm on Jan 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A better solution might be to store a dirty (dirty meaning changed) variable.

<head>
<script language="javascript">
var dirty=0;
function chkDirty() {
if (dirty == 1) { alert('Save your changes'); }
}
</script>
</head>
body onUnload="chkDirty();">

<form>
<input type="text" name="element1" onChange="dirty=1" value="change me">
</form>

Untested, but that should work. Won't help a bit if they close the window, but will help if they navigate off.