2

By pressing Enter key need to submit the form in angular 4 below is my code in form Action = "" is not working. I also tried with (keydown) = domonething(event) and (keydown.enter) = domonething(event) with below code

keyDownFunction(event) {
    if (event.keyCode == 13) {
        this.submit();
    }
}

Below is my current code

<form #f="ngForm" (ngSubmit)="f.form.valid && openModal(confirmationmodal)" novalidate action="">
    <div class="form-group form-row">
        <label class="col-form-label col-sm-4 col-md-4" for="name">Employee Name</label>
        <div class="col-sm-8 col-md-8">
            <span type="text" readonly class="form-control-plaintext" id="name">{{employeeDetails.EmployeeName}}</span>
        </div>
    </div>
    <div class="form-group form-row">
        <label class="col-form-label col-sm-4 col-md-4 " for="name">Manager Name</label>
        <div class="col-md-8  col-sm-8">
            <span type="text" readonly class="form-control-plaintext"
                id="manager">{{employeeDetails.ManagerName}}</span>
        </div>
    </div>
    <div class="form-group form-row">
        <label for="name" class="col-sm-4 col-md-4 col-form-label">Subject</label>
        <div class="col-md-8 col-sm-8">
            <span type="text" readonly class="form-control-plaintext" id="manager">{{subject}}</span>
        </div>
    </div>

    <button class="btn btn-success float-right" type="submit" id="submit">Submit</button>
</form>
James Coyle
  • 9,922
  • 1
  • 40
  • 48
Satti
  • 559
  • 2
  • 8
  • 26
  • It's duplicate of https://stackoverflow.com/questions/37577919/angular2-submit-form-by-pressing-enter-without-submit-button – Chintan Kukadiya Mar 25 '19 at 08:59
  • Possible duplicate of [angular2 submit form by pressing enter without submit button](https://stackoverflow.com/questions/37577919/angular2-submit-form-by-pressing-enter-without-submit-button) – Shofol Mar 25 '19 at 09:10
  • the above comment is also a duplicate of the one above that – danday74 Nov 24 '21 at 12:38

4 Answers4

8

give (keyup.enter)="yourFunction()" in your submit button

Seba Cherian
  • 1,755
  • 6
  • 18
1

From your form tag I would remove the action attribute and put the following:

<form #f="ngForm" (ngSubmit)="f.form.valid && openModal(confirmationmodal)" novalidate (keydown.enter)="onEnterKeyDown($event)">

Then simply writing the key down event's function:

onEnterKeyDown($event) {
  // here you can open your confirmation modal if the form is valid
}

Source: https://alligator.io/angular/binding-keyup-keydown-events/

norbitrial
  • 14,716
  • 7
  • 32
  • 59
0

this should work

(keydown)="keyDownFunction($event)

in typescript

  keyDownFunction(event) {
    if ( event.keyCode === 13) {
      // do your function
    }
  }

this works for me. I think you've missed $ in front of event.

GreedyAi
  • 2,709
  • 4
  • 29
  • 55
0

If you want to invoke it with the button then you can do like this-

<button type="button" (keyup.enter)="doSomething()" >Click Me!</button>

See live here- https://stackblitz.com/edit/angular-3gf6hw

See more about it here- https://angular.io/guide/user-input#key-event-filtering-with-keyenter

Shofol
  • 693
  • 1
  • 11
  • 26