Reputation: 73
I have two input fields and button. when I press Enter in one of the two input fields supposed to execute some code but what happens the code of button is executed
<div class="input-group">
<button mat-raised-button color="primary"
(click)="getCurrency()"
style="margin-right: 10px;width: 20px">...</button>
<div class="input-group-prepend">
<input id="currencyDesc" type="text" size="15" [(ngModel)]='name' [ngModelOptions]="{standalone: true}">
</div>
<div class="input-group-prepend">
<input formControlName="CRCYIsn" type="text" size="4" (keyup.enter)="currencyByIsn()">
</div>
<label> : العملة</label>
</div>
Upvotes: 2
Views: 94
Reputation: 17494
Try (keydown.enter)="$event.preventDefault()"
as below on that button:
<button mat-raised-button color="primary"
type="button"
(keydown.enter)="$event.preventDefault()"
(click)="getCurrency()"
style="margin-right: 10px;width: 20px">...</button>
If it's a form, try to put this code on the <form>
tag itself
Upvotes: 3
Reputation: 73337
Buttons inside forms are by default of type submit
. Tell the button to be of type button
and it shouldn't be fired:
<button mat-raised-button type="button" color="primary" (click)="getCurrency()">
Also like mentioned, don't mix reactive forms with ngModel
.
Upvotes: 0
Reputation: 71
I will change the next things:
Upvotes: 0
Reputation: 519
For ENTER key, why not use (keyup.enter)
<div class="input-group">
<button mat-raised-button color="primary"
(click)="getCurrency()"
style="margin-right: 10px;width: 20px">...</button>
<div class="input-group-prepend">
<input id="currencyDesc" (keyup.enter)="doSomething()" type="text" size="15" [(ngModel)]='name' [ngModelOptions]="{standalone: true}">
</div>
<div class="input-group-prepend">
<input formControlName="CRCYIsn" (keyup.enter)="doSomething()" type="text" size="4" (keyup.enter)="currencyByIsn()">
</div>
<label> : العملة</label>
</div>
Upvotes: 0