views:

28

answers:

1

I use jQueryUI's tabs and when a user changes a form in the tab, it promprts the user that a change has been made.

My question is, how do I revert the values before being changed when the user presses 'OK' on my confirm() prompt?

A: 

You'll have to keep track of what the values were before changing them... so something like this should work for you:

var oldValues = {};
$(function() {
    $(":input").each(function() {
        oldValues[$(this).attr("id")] = $(this).val();
    });
});
function revertValues() {
    for (var oldVal in oldValues) {
        $("#" + oldVal).val(oldValues[oldVal]);
    }
}

And then just call revertValues when you hit OK in your confirmation dialog.

A few notes:

  • I just wrote this code straight into the browser, so it's completely untested...
  • Assuming you have other inputs on other tabs, you'll need to change the :input selector and keep different sets of oldValues for each tab (& then obviously only revert the values for the current tab.
Alconja