How to trigger button click on enter key press in input text with JavaScript

1 Answer

0 votes
var input = document.getElementById("input_id");
input.addEventListener("keyup", function(event) {
    if (event.keyCode === 13) {
        event.preventDefault();
        document.getElementById("submit_button").click();
    }
});
 
 
 
/*
run:
 
button click
    
*/
<body> 
<form>
    <input id="input_id" placeholder="What's on your mind, User?" value="">
    <input type="submit" id="submit_button" onclick="alert('button click')" value="Submit">
</form>
    </body> 

 



answered Sep 15, 2019 by avibootz
edited Sep 16, 2019 by avibootz
...