There is a button and input:

<div class="img"> <button></button> <input type="text" class="my-input"> </div> 

It is necessary that when you press the "Enter" key, the button button is pressed, and the input field is cleared (if there was a text in it).

  • Yes, but I need more so that after pressing Enter, the input text field is cleared. How to do it? - A.Hall
  • use button [type = "reset"] - soledar10
  • How exactly to use it? - A.Hall
  • Hang up on event onkeyup element body function handler. If the keyCode of the pressed button is enter, then all the functions that are performed when you press your button will be executed - Dimon

2 answers 2

Set Id button and input, substitute in the code.

 $("#id_of_textbox").keyup(function(event){ if(event.keyCode == 13){ $("#id_of_button").click(); $(this).val(''); } }); 

The code from this answer, the link to which was given above: https://stackoverflow.com/questions/155188/trigger-a-button-click-with-javascript-on-the-enter-key-in-a-text-box

    Example:

     $(document).keypress(function (e) { if (e.which == 13) { document.getElementById("my-button").click(); document.getElementById("my-input").value = ""; alert("Pressed"); } }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <div class="img"> <button id="my-button"> Press Me</button> <input id="my-input" type="text" class="my-input"> </div>