Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Trigger a Button Click on Enter Key Press in a Text Input</title> <script> function showHint(){ document.getElementById("hint").innerHTML += "<p>The Enter key is pressed.</p>" } document.addEventListener("DOMContentLoaded", function(){ document.getElementById("myInput").addEventListener("keypress", function(event){ if(event.keyCode == 13){ document.getElementById("myBtn").click(); } }); }); </script> </head> <body> <p><strong>Note:</strong>Type anything into the text box below and press the Enter key on the keyboard.</p> <p><input type="text" id="myInput"></p> <p><button type="button" id="myBtn" onclick="showHint();">My Button</button></p> <div id="hint"></div> </body> </html>