Reset LOGIN without FORM

I have an INPUT element and I want to reset the previous value (optional initial value). There are two ways:

  • Save the value in another variable and pull it out again
  • Press the ESC key. But I do not want users to press ESC, but press a button.

So for # 2, how can I create an ESC push using jquery?

+5
source share
4 answers

Here is SSCCE :

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2079185</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $(':input.remember').each(function() {
                    $(this).attr('data-remember', $(this).val());
                });
                $('button.reset').click(function() {
                    $(':input.remember').each(function() {
                        $(this).val($(this).attr('data-remember'));
                    });
                });
            });
        </script>
    </head>
    <body>
        <input type="text" class="remember" value="foo">
        <button class="reset">Reset</button>
    </body>
</html>

This basically saves the original value of each input element with the class rememberduring onload and instructs the button with the class resetto restore it every time it is clicked.

+5
source

Esc , .

, , , .

№1.

(, ) , , .

+2

, . ( ) , ; , .

, , Escape - quirk IE: Escape , IE . , Escape , Escape . , <input type="reset"> , Escape reset. , , reset.

, reset , form.reset(). reset , input.value= input.defaultValue ( input.checked= input.defaultChecked / defaultSelected ).

If, however, you need a value that was present at the time of the last focused field, since IE behaves when there is no reset button, you will need to do some memorization of variables, for example:

var undonevalue= $('#undoable').val();
$('#undoable').focus(function() {
    undonevalue= $('#undoable').val();
});
$('#undoer').click(function() {
    $('#undoable').val(undonevalue);
});
+2
source

Each element (input, flag, radio) has an attribute that contains the default value, the element has been initialized. This script does not work for previous values.

// pseudocode
input.defaultValue;
radio.defaultChecked;
checkbox.defaultChecked;

To set the default value for input by pressing the ESC key:

$(document).ready(function(){
    // reset by pressing ESC with focus on input
    $('input').keypress(function(e) {
        if (e.keyCode == 27) {
            this.value = this.defaultValue;
        }
    });
});
+1
source

All Articles