How to submit form on pressing Enter with Angular 9? Last Updated : 05 Nov, 2020 Comments Improve Suggest changes 2 Likes Like Report To submit a form in angular we have the following options: Create a button to submit the form.Assign a key to submit the formOr we can do both. In this tutorial, we will see how to use a specific key(Enter in this case) to submit a form. Approach: We can use angular keydown event to use Enter key as our submit key. Add keydown directive inside the form tag.Create a function to submit the form as soon as Enter is pressed.Assign the keydown event to the function. Example: Let's create a form having both, button and Enter key as mode of form submission. We will use bootstrap classes, so add bootstrap scripts in your index.html. html <html lang="en"> <head> <meta charset="utf-8" /> <title>Tutorial</title> <!--add bootstrap script here--> <link rel="stylesheet" href= "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity= "sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous" /> <script src= "https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity= "sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src= "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity= "sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src= "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity= "sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> </head> <body> <app-root></app-root> </body> </html> code for the component: javascript import { Component } from '@angular/core'; @Component({ selector: 'app-root', //here we used inline template format. template: ` <div style="text-align: center;"> <h1> {{title}} </h1> </div> <!--using keydown to assign the event to call EnterSubmit method--> <form #geeksForm="ngForm" (keydown)="EnterSubmit($event, geeksForm.form)" (ngSubmit)="submitit(geeksForm.form);"> <button class="btn btn-primary" [disabled]="!geeksForm.valid"> Submit </button> <input type="text" class="form-control" name="geek-name" ngModel #geekname="ngModel" required minlength="5" /> <div *ngIf="geekname.errors.required"> The geek name is required </div> <div *ngIf="geekname.errors.minlength"> The geek name should be at least {{ geekname.errors.minlength.requiredLength }} characters long </div> <select class="form-control" name="geek-type" ngModel #geeksField="ngModel" required> <option *ngFor="let geek of geeks" [value]="geek.id"> {{ geek.name }} </option> <div *ngIf="geeksField.touched && !geeksField.valid"> The category is required </div> `, styleUrls: [] }) export class AppComponent { title = 'Form submission tutorial'; public name = "geek"; geeks = [ {id: 1, name: "c++geek"}, {id: 2, name: "pythongeek"}, {id: 3, name: "javageek"}, {id: 4, name: "javascriptgeek"}, {id: 5, name: "angulargeek"} ]; /*assigning EnterSubmit function to keydown event and using Enter key to submit the form. */ //Function will take two parameters: //1.The key pressed. //2.form. EnterSubmit(event, form) { //keycode for Enter is 13 if (event.keyCode === 13) { alert('Enter key is pressed, form will be submitted'); //calling submit method if key pressed is Enter. this.submitit(form); } } //function to submit the form submitit(form){ console.log(form.value); alert("The form was submitted"); form.reset(); } } </select> </form> Output: Create Quiz Comment V vaibhav19verma Follow 2 Improve V vaibhav19verma Follow 2 Improve Article Tags : Web Technologies AngularJS AngularJS-Misc Explore AngularJS BasicsAngularJS Tutorial 5 min read Introduction to AngularJS 4 min read Angular CLI | Angular Project Setup 3 min read AngularJS Expressions 2 min read AngularJS Modules 3 min read AngularJS ng-model Directive 4 min read AngularJS Data Binding 4 min read AngularJS Controllers 3 min read AngularJS | Scope 2 min read AngularJS Services 4 min read AngularJS | AJAX - $http 3 min read AngularJS | Tables 2 min read AngularJS Select Boxes 2 min read AngularJS SQL 3 min read AngularJS HTML DOM 2 min read AngularJS Events 3 min read AngularJS | Forms 3 min read AngularJS Form Validation 3 min read AngularJS | API 2 min read AngularJS and W3.CSS 2 min read AngularJS Includes 3 min read AngularJS Animations 1 min read AngularJS | Application 3 min read AngularJS DirectivesAngularJS Directives 9 min read AngularJS ng-app Directive 1 min read AngularJS ng-bind Directive 2 min read AngularJS ng-bind-html Directive 2 min read AngularJS ng-bind-template Directive 2 min read AngularJS ng-blur Directive 1 min read AngularJS ng-change Directive 2 min read AngularJS ng-checked Directive 2 min read AngularJS ng-class Directive 2 min read AngularJS ng-class-even Directive 2 min read AngularJS ng-class-odd Directive 2 min read AngularJS ng-click Directive 2 min read AngularJS ng-cloak Directive 2 min read AngularJS ng-controller Directive 2 min read AngularJS Directives Complete Reference 2 min read AngularJS FiltersAngularJS | Filters 7 min read AngularJS currency Filter 2 min read AngularJS | date Filter 2 min read AngularJS filter Filter 3 min read AngularJS json Filter 2 min read AngularJS limitTo Filter 2 min read AngularJS lowercase Filter 1 min read AngularJS number Filter 1 min read AngularJS orderBy Filter 4 min read AngularJs uppercase Filter 1 min read AngularJS Converting FunctionsAngularJS angular.lowercase() Function 2 min read AngularJS angular.uppercase() Function 1 min read AngularJS angular.forEach() Function 1 min read AngularJS Comparing FunctionsAngularJS angular.isArray() Function 2 min read AngularJS angular.isDate() Function 2 min read AngularJS angular.isDefined() Function 2 min read AngularJS angular.isElement() Function 2 min read AngularJS angular.isFunction() Function 2 min read AngularJS angular.isNumber() Function 2 min read AngularJS angular.isObject() Function 2 min read AngularJS | angular.isString() Function 1 min read AngularJS angular.isUndefined() Function 2 min read AngularJS angular.equals() Function 2 min read AngularJS angular.toJson() Function 2 min read AngularJS QuestionsHow to bundle an Angular app for production? 4 min read How to add many functions in one ng-click directive? 2 min read How to directly update a field by using ng-click in AngularJS ? 3 min read How to Add Dynamic Options for Multiple Selects Inside ng-repeat Directive ? 3 min read How to detect when an @Input() value changes in Angular? 3 min read How to open popup using Angular and Bootstrap ? 2 min read How to reload or re-render the entire page using AngularJS? 2 min read How to add input fields dynamically on button click in AngularJS ? 2 min read How to Create Button Dynamically with Click Event in Angular ? 2 min read How to use jQuery in Angular ? 2 min read AngularJS Examples 2 min read Like