I have one text input and one button (see below). How can I use JavaScript to trigger the button\'s click event when the Enter key is pressed ins
In jQuery, you can use event.which==13
. If you have a form
, you could use $('#formid').submit()
(with the correct event listeners added to the submission of said form).
$('#textfield').keyup(function(event){
if(event.which==13){
$('#submit').click();
}
});
$('#submit').click(function(e){
if($('#textfield').val().trim().length){
alert("Submitted!");
} else {
alert("Field can not be empty!");
}
});