How to get value from input field of html form at input?

How can I get a value from an input field when I enter it?

+2
source share
3 answers

onkeyup will run every time a key is released. Although this looks like a solution, it has some problems.

If the user moves the cursor with arrows, it starts, and you must check yourself if the field value has changed.

If the user copies / pastes the value into the input field using the mouse or cancels the cancel / redo in the browser, onkeyup does not start.

Mac google, , , . , .

  • onfocus,
  • - , ,
  • onblur, ,

, , elm - , - , :

<html>
<head>
    <title>so</title>
</head>
<body>
    <input type="text" onfocus="changeField(this, fldChanged);">
    <script>
        function changeField(elm, after){
            var     old, to, val,
                chk = function(){
                    val = elm.value;
                    if(!old && val === elm.defaultValue){
                        old = val;
                    }else if(old !== val){
                        old = val;
                        after(elm);
                    }
                };
            chk();
            to = setInterval(chk, 400);
            elm.onblur = function(){
                to && clearInterval(to);
                elm.onblur = null;
            };
        };
        function fldChanged(elm){
            console.log('changed to:' + elm.value);
        }
    </script>
</body>
</html>
+5

, jquery. jQuery .keypress().

API:

: "" JavaScript .

, .

.keydown() .keyup() . .keypress() API.

jQuery , Firefox, IE, Safari, Opera Chrome.

+2

Source: https://habr.com/ru/post/1734024/


All Articles