The value of the input form form for "" does not clear the spaces

I am trying to implement a command line emulator in JavaScript. I want the key to Spacebaract like Enter(send, then clear the command line).

In this message, denote spaceas an underscore, therefore _means [space]:

If I type banana_, and then, after capturing a keystroke, I call input.value= "", it clears banana, but not the rest space.

I tested both onkeydown, and so onkeypress, but it doesn't seem to make any difference. Actually onkeyupdoing the trick, see my answer below!

This is my code. Can someone please help me remove the extra space ...

<html><head>

<script type="text/javascript">  

    document.onkeypress = keyCheck;  

    function keyCheck( e ) {  
        var keyID = (window.event) ? event.keyCode : ( e.keyCode ? e.keyCode : e.charCode );  
        switch(keyID) {  
            case 32: //spacebar  
                alert( "space pressed, clearing..." );  
                document.getElementById( "cli" ).value="";  
                break;  
            default:  
                //do something else  
                break;  
            } 
        }  
</script>  
</head>  

<body>
  <form name="frm" id="frm" onSubmit="go( this )">
  <input name="cli" id="cli" type="text" name="cmd" size="122">
</body>

</html>
+3
1

onkeyup .

document.onkeyup = keyCheck;

, , .

: http://jsbin.com/esacu3

+2

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


All Articles