How to get the form to be sent to the input key only when the user focuses on the input login?

I have 3 fields on the page, and when the user has a cursor focused on two of them, I want a certain button to be pressed on the page, which was pressed when they press the enter button. I use

<script language="JavaScript"> 

 function stopRKey(evt) {
         var evt = (evt) ? evt : ((event) ? event : null);
         var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
         if ((evt.keyCode == 13) && (node.type == "text")) { return false; }
     }

     document.onkeypress = stopRKey;
</script>

To stop the default behavior, I just don't see how to change it to skip some keystrokes. I looked through the http://asquare.net/javascript/tests/KeyCode.html tutorial , but it was not obvious what was happening.

Does anyone know how to do this or a tutorial that goes through this?

+3
source share
2

jQuery, :

<script language="javascript"> 

document.getElementById('myInput1').addEventListener('keypress', handlePress);
document.getElementById('myInput2').addEventListener('keypress', handlePress);

function handlePress(evt) 
{
    if( evt.keyCode == 13 )
    {
        // execute your function here
        console.log('Enter pressed while in desired text input')
    }
}
</script>
+1

jQuery ( ):

$('#input_id').keypress(function(event) {
  if (event.keyCode == '13') {
       // press correct button
   }
});

.

+2

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


All Articles