139

Is it possible to submit a form that does not have submit button (by pressing enter) example :

<form [ngFormModel]="xxx" (ngSubmit)="xxxx()">
  <input [(ngModel)]="lxxR"   ngControl="xxxxx"/>
</form
Rosdi Kasim
  • 24,267
  • 23
  • 130
  • 154
Florence
  • 1,641
  • 3
  • 13
  • 23

16 Answers16

268

You can also add (keyup.enter)="xxxx()"

sizzle
  • 2,883
  • 2
  • 13
  • 10
101

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
      }
    }
raaaay
  • 496
  • 7
  • 14
Khaled Al-Ansari
  • 3,910
  • 2
  • 24
  • 27
97

Edit:

  <form (submit)="submit()" >
    <input />
    <button type="submit" style="display:none">hidden submit</button>
  </form>

In order to use this method, you need to have a submit button even if it's not displayed "Thanks for Toolkit's answer"

Old Answer:

Yes, exactly as you wrote it, except the event name is (submit) instead of (ngSubmit):

<form [ngFormModel]="xxx" (submit)="xxxx()">
  <input [(ngModel)]="lxxR"   ngControl="xxxxx"/>
</form>

Community
  • 1
  • 1
Abdulrahman Alsoghayer
  • 16,462
  • 7
  • 51
  • 56
42

I prefer (keydown.enter)="mySubmit()" because there won't be added a line break if the cursor was somewhere within a <textarea> but not at its end.

Martin Schneider
  • 14,263
  • 7
  • 55
  • 58
40

This way is much simple...

<form [formGroup]="form" (keyup.enter)="yourMethod(form.value)">

</form>
Clayton K. N. Passos
  • 1,292
  • 12
  • 15
12

add this inside your input tag

<input type="text" (keyup.enter)="yourMethod()" />
Abdus Salam Azad
  • 5,087
  • 46
  • 35
11
(keyup.enter)="methodname()"

this should work and already mentioned in many answers, however that should be present on form tag and not on button.

Santosh Kadam
  • 1,265
  • 14
  • 14
10

Always use keydown.enter instead of keyup.enter to avoid lagging.

<textarea [(ngModel)]="textValue" (keydown.enter)="sendMessage();false" ></textarea>

and function inside component.ts

 textValue : string = '';  
 sendMessage() {
    console.log(this.textValue);
    this.textValue = '';
  }
Shamsul
  • 435
  • 6
  • 15
9

Most answers here suggest using something like:

<form [formGroup]="form" (ngSubmit)="yourMethod()" (keyup.enter)="yourMethod()">

</form>

This approach does not result in the form object being marked as submitted. You might not care for this, but if you're using something like @ngspot/ngx-errors (shameless self-promotion) for displaying validation errors, you gonna want to fix that. Here's how:

<form [formGroup]="form" (ngSubmit)="yourMethod()" (keyup.enter)="submitBtn.click()">
  <button #submitBtn>Submit</button>
</form>
Dmitry Efimenko
  • 10,973
  • 7
  • 62
  • 79
8

Just simply add (keyup.enter)="yourFunction()"

Saurabh Agrawal
  • 7,581
  • 2
  • 27
  • 51
7

adding an invisible submit button does the trick

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

Toolkit
  • 10,779
  • 8
  • 59
  • 68
4

Hopefully this can help somebody: for some reason I couldn't track because of lack of time, if you have a form like:

<form (ngSubmit)="doSubmit($event)">
  <button (click)="clearForm()">Clear</button>
  <button type="submit">Submit</button>
</form>

when you hit the Enter button, the clearForm function is called, even though the expected behaviour was to call the doSubmit function. Changing the Clear button to a <a> tag solved the issue for me. I would still like to know if that's expected or not. Seems confusing to me

Andrea Gherardi
  • 826
  • 1
  • 9
  • 18
4

If you want to include both simpler than what I saw here, you can do it just by including your button inside form.

Example with a function sending a message:

                <form>
                    <mat-form-field> <!-- In my case I'm using material design -->
                        <input matInput #message maxlength="256" placeholder="Message">
                    </mat-form-field>
                    <button (click)="addMessage(message.value)">Send message
                    </button>
                </form>

You can choose between clicking on the button or pressing enter key.

Emeric
  • 6,315
  • 2
  • 41
  • 54
1

If you want to include both - accept on enter and accept on click then do -

 <div class="form-group">
    <input class="form-control"   type="text" 
    name="search" placeholder="Enter Search Text"
              [(ngModel)]="filterdata"  
           (keyup.enter)="searchByText(filterdata)">
  <button type="submit"  
          (click)="searchByText(filterdata)" >

  </div>
1

Well, there's a small caveat here, I would say. None of the above would work if you want to submit the form until unless you click anywhere on the form or specifically on the input field.

Well if you want to submit the form just by hitting enter without clicking anywhere, you need to declare it globally in html:

<button (window:keypress)="onSomeAction($event)">

and in TS file :

onSomeAction(event){
if(event.keyCode===13){
  //submit form
}

}

OR

<button (window:keypress.enter)="onSomeAction($event)">
Kamil Kafoor
  • 256
  • 3
  • 8
0
<form [ngFormModel]="xxx" (keyup.enter)="xxxx()" (ngSubmit)="xxxx()">
  <input [(ngModel)]="lxxR"   ngControl="xxxxx"/>
</form>
nisith
  • 11
  • 3