0

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

Marcus Recck
  • 5,075
  • 2
  • 16
  • 26
DotnetSparrow
  • 27,428
  • 62
  • 183
  • 316

3 Answers3

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

I would use onchange event with HTML5 localStorage

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