0

As I am beginner in HTML, could someone make a logical explanation on the following? I can submit data using <button type="submit">, but if I had a single <form type="text">, I could submit it just by pressing enter.

As I failed to gather any info on that, does someone know how does it happen? Wouldn't it make sense to always have a button that would submit, and not also waiting for the user's enter? It feels as if enter on keyboard is same as button submit. Example:

<form action="https://www.youtube.com/results">
    <input type="text" name="search_query">
    <button>Submit me!</button>
</form>

This one also works by pressing just enter:

<form action="https://www.youtube.com/results">
    <input type="text" name="search_query">
</form>

P.S. Of course on both these input types we have to type dog.

Stefan
  • 969
  • 6
  • 9
  • what is your question ? – Mister Jojo Nov 28 '20 at 01:19
  • [Possible duplicate](https://google.com/search?q=site%3Astackoverflow.com+submit+html+form+by+pressing+enter) of [Submitting a form by pressing enter without a submit button](https://stackoverflow.com/q/477691/4642212), but it depends on your exact requirements; e.g. there’s a way to do this with or without JavaScript, do it well or do it badly, etc. – Sebastian Simon Nov 28 '20 at 01:22
  • I can't understand what you are asking. – Spectric Nov 28 '20 at 02:04

1 Answers1

1

You can submit the form on an event with javascript like this:

document.getElementById("myFormId").submit();

If you want to disable the submitting of a form on enter, you can also do this with JS.

document.getElementById("myFormId").onkeypress = function(e) {  
//When key is pressed in form
  if (e.keyCode == 13){
  //If key is equal to enter (key code 13)
    e.preventDefault();
    //Prevent the default action (submitting the form)
  }
}
Tabulate
  • 611
  • 6
  • 19