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.
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)
}
}