Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to disable input clear by ESC button, Bootstrap-5

I have this piece of code for search field:

<form class="container" role="search">
    <div class="row">
      <div class="col-12">
        <input name="searchItem" #searchInput="ngModel" ngModel
          (ngModelChange)="searchUserShowCpv(searchInput.value)" class="form-control me-2" type="search"
          placeholder="Search" aria-label="Search">
      </div>
    </div>
  </form>

The problem is that if there is some value in the field and the ESC button is pressed, the field is cleared, but the ngModelChange event for an empty field value does not fire.

How to disable field clearing when pressing Esc?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As mentioned in the doc. This is how input type="search" works.

You can add (keydown.escape)="$event.preventDefault()" to input element to prevent clearing on escape button click

  <input name="searchItem" #searchInput="ngModel" ngModel
      (keydown.escape)="$event.preventDefault()"
        (ngModelChange)="searchUserShowCpv(searchInput.value)" class="form-control me-2" type="search"
        placeholder="Search" aria-label="Search">
    </div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading