1
2 // Get the input field
3var input = document.getElementById("myInput");
4
5
6input.addEventListener("keyup", (event) => {
7 if (event.key === "Enter") {
8 // Cancel the default action, if needed
9 event.preventDefault();
10 // Trigger the button element with a click
11 document.getElementById("myBtn").click();
12 }
13})
1<form>
2 <input id="myInput" placeholder="Some text.." value="">
3 <input type="submit" id="myBtn" value="Submit">
4</form>
5
6<script>
7var input = document.getElementById("myInput");
8input.addEventListener("keyup", function(event) {
9 if (event.keyCode === 13) {
10 event.preventDefault();
11 document.getElementById("myBtn").click();
12 }
13});
14</script>
15