jQuery how to undo a select change

Rather than using a global variable (evil!) or a hidden element (abuse of the DOM) you can use the $.data feature:

$('select').change(function() {
    var selected = $(this).val();

    if (selected == 'bar') {
        if (!confirm('Are you sure?')) {
            $(this).val($.data(this, 'current'));
            return false;
        }     
    }

    $.data(this, 'current', $(this).val());
});

Leave a Comment