Search code examples
angularclickkeyup

pressing enter calls the code in click method


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>

Solution

  • 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