-2

How do I submit a form on enter with type="button" This is my input field

<input type="text" id = "login-user" class="form-control log-input" placeholder="Username" required="required">
<input type="password" id="login-password" class="form-control log-input" placeholder="Password" required="required">                   
<input type="button" id = "login-submit" class="btn btn-primary log-submit" value="Login" onclick = loginsubmit()>

I do not want to use type="submit" can I do it with type="button" or any other approach?

shee
  • 165
  • 1
  • 10
  • You can do it with javascript. Bind your js function to onClick attribute. – anmatika Sep 14 '21 at 06:45
  • 1
    Does this answer your question? [Submit form with Enter key without submit button?](https://stackoverflow.com/questions/8981637/submit-form-with-enter-key-without-submit-button) – Lain Sep 14 '21 at 06:47

2 Answers2

0

You can use the code below to submit a form after pressing Enter.

<input id="videoid" placeholder="Enter the video ID">
<button id="mybutton" type="button" onclick="myFunction()">Submit</button>

<script>
var input = document.getElementById("videoid");
input.addEventListener("keyup", function(event) {
  if (event.keyCode === 13) {
   event.preventDefault();
   document.getElementById("mybutton").click();
  }
});
</script>
Eduard
  • 311
  • 1
  • 4
0

Using jQuery you would do something like this:

// Catch enter key stroke in input field
$('#somefield').on('keyup', (e) => {
  if (e.code === 13 || e.keyCode === 13) {
    e.preventDefault()
    // Submit your form
    $('#form').submit()
  }
})
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
  • KeyCode and submit() is deprecated do we have any other option – shee Sep 14 '21 at 06:59
  • I updated to use the `code` property if it exists, but not sure why you say the `submit()` is deprecated? Not what I can see in the documentation? – Cyclonecode Sep 14 '21 at 07:16