I have a textarea on my asp.net mvc view and two buttons Save and Cancel. I want to check if user typed something in textarea but didnt click save and click cancel buttton. or click save button and then changed the text and didnt click save after change and click cancel. on cancel click I want to show alert but how can i check if user didnt click save after changing the text using jquery or javascript (perhaps in .change event or cancel click ) Please suggest
Asked
Active
Viewed 1,991 times
0
-
1Did you ever hear of `defaultValue`? – epascarello Feb 13 '13 at 18:51
-
Is this your requirment - http://stackoverflow.com/questions/11844256/alert-for-unsaved-changes-in-form – ssilas777 Feb 13 '13 at 19:13
3 Answers
0
You could have a hidden field that you set to some value when a user saves, then just check the hidden field for a value using jquery or javascript...

Kixoka
- 989
- 4
- 15
- 37
0
Javascript:
<script>
$(function() {
$("#TextAreaName").change(function() {
$(this).attr("data-changed", true);
});
$("#Save").click(function(event) {
event.preventDefault();
if ($("#TextAreaName").attr("data-changed") === "true") {
//Do your saving here
//Reset the state
$("#TextAreaName").attr("data-changed", false);
}
});
});
Razor markup
<body>
<div>
@using (Html.BeginForm())
{
@Html.TextArea("TextAreaName", new{data_changed = false})
<button id="Save" type="submit">Save</button>
<button id="cancel" type="button">Cancel</button>
}
</div>

amhed
- 3,649
- 2
- 31
- 56
-
I like the html5 data attributes option better, since there isn't a need for an additional element in the DOM – amhed Feb 13 '13 at 19:20