Submitting a Form by Pressing Enter Without a Submit Button

Submitting a form by pressing enter without a submit button

Update 2022: Use this instead

<input type="submit" hidden />














Notice - Outdated answer
Please do not use position: absolute in the year 2021+. It's recommended to use the hidden attribute instead. Otherwise, look down below and pick a better, more modern, answer.

HTML5: How to make a form submit, after pressing ENTER at any of the text inputs?

Try adding this between the <form></form> tags

<input type="submit" style="display: none" />

Tested it and it works on Firefox and Chrome. If you have a submit input type in the form, enter should automatically submit it, regardless of whether it's visible or not.


I am actually using this myself in a login form, though in the username field, it makes more sense to move to the next field than to submit. Just in case you have a similar use case, here's the code I used (requires jQuery)

$('#username').keypress(function(event) {
if (event.keyCode == 13 || event.which == 13) {
$('#password').focus();
event.preventDefault();
}
});

Note that there is a slight bug though -- if the user selects a browser autocomplete username and presses enter, it still moves to the next field instead of selecting it. Didn't have time to debug this, but if someone can figure out how to fix it, that would be cool.

Prevent users from submitting a form by hitting Enter

You can use a method such as

$(document).ready(function() {
$(window).keydown(function(event){
if(event.keyCode == 13) {
event.preventDefault();
return false;
}
});
});

In reading the comments on the original post, to make it more usable and allow people to press Enter if they have completed all the fields:

function validationFunction() {
$('input').each(function() {
...

}
if(good) {
return true;
}
return false;
}

$(document).ready(function() {
$(window).keydown(function(event){
if( (event.keyCode == 13) && (validationFunction() == false) ) {
event.preventDefault();
return false;
}
});
});

angular2 submit form by pressing enter without submit button

Maybe you add keypress or keydown to the input fields and assign the event to function that will do the submit when enter is clicked.

Your template would look like this

    <form (keydown)="keyDownFunction($event)">
<input type="text" />
</form

And you function inside the your class would look like this

    keyDownFunction(event) {
if (event.keyCode === 13) {
alert('you just pressed the enter key');
// rest of your code
}
}

Submit form with Enter key without submit button?

$("input").keypress(function(event) {
if (event.which == 13) {
event.preventDefault();
$("form").submit();
}
});

How to submit form by pressing enter or pressing submit button?

Instead of using <input type="button" />, use <input type="submit" /> to send the form.

The enter button should submit the form by default.

Prevent users from submitting a form multiple times by pressing the ENTER button

This will only allow it to submit once.

$('#form_id').on('submit', function() {
$(this).on('submit', function() {
return false;
});

$('#submit_button_id').hide();
$('#hidden_div_id').show();
return true;
});

How to submit a form by pressing Enter Key from any field in the form in Angular Material?

you should be able to just bind to the ngSubmit event:

<form class="example-form" [formGroup]="docForm" (ngSubmit)="docForm.valid && docForm.dirty && update()">

for this to trigger, you need a submit type element somewhere inside your form, it may be hidden though:

<input [style.display]="'none'" type="submit">

or if you prefer, you may just bind to the keyup.enter event on the form:

<form class="example-form" [formGroup]="docForm" (keyup.enter)="docForm.valid && docForm.dirty && update()">

How can I submit form's data without button in HTML?

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


Related Topics



Leave a reply



Submit