Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submitting a form by pressing enter key in angular 4

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>
like image 647
Satti Avatar asked Jan 01 '26 07:01

Satti


2 Answers

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

like image 88
Seba Cherian Avatar answered Jan 02 '26 21:01

Seba Cherian


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/

like image 38
norbitrial Avatar answered Jan 02 '26 21:01

norbitrial



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!