0

I'm trying to detect all textboxes and dropdownlists on my page and check if they have changes upon window unload. The problem is, I can detect textboxes but not dropdownlists. Here's how I do it.

$(document).ready(function () {
        var warnMessage = 'Warning: You have unsaved changes.';
        $('input:not(:button,:submit),input[type="text"],select').each(function () {
            var elem = $(this);

            // Save current value of element
            elem.data('oldVal', elem.val());

            // Look for changes in the value
            elem.bind("propertychange keyup input paste", function (event) {
                // If value has changed...
                if (elem.data('oldVal') != elem.val()) {
                    window.onbeforeunload = function () {
                        if (warnMessage != null) return warnMessage;
                        else warnMessage = 'Warning: You have unsaved changes.';
                    }
                }
            });

        });
        $('input:submit,#addLiquidFillButton,#addCommentButton,#addManualRegenButton,#addStateMileageButton').click(function (e) {
            warnMessage = null;
        });
    });

Additional Info. I create dropdownlist using this code:

<asp:DropDownList ID="LocationIdDDL" CssClass="ddl" runat="server" AutoPostBack="true"></asp:DropDownList>

I'm assumed that by adding 'select' will check all dropdownlists, but right now, it don't. Any ideas? Thanks!

2 Answers 2

1

Another approach might be to just set an onchange to all inputs which set a boolean variable when its changed, then just check that boolean variable, rather than trying to compare each actual value?

You issue might be the "propertychange" event, is it not just "change"

Also, your dropdown list is doing a postback, maybe that is bypassing your event?

Sign up to request clarification or add additional context in comments.

1 Comment

I changed "propertychange" to just "change" and it worked. Thanks!
0

remove the below dropdownlist

<asp:DropDownList ID="LocationIdDDL" CssClass="ddl" runat="server" AutoPostBack="true"></asp:DropDownList>

and use this:

<select id="LocationIdDDL"><option><option></select>

1 Comment

I'd rather not change the way I created it. This will be a big change. I really need to use ASP.NET controls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.