Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 How to blur input on button return?

How to blur input by pressing native keyboard return button on mobile ?

For example :

<input type="text" #search>

this.search.blur() //-- unfocus and hide keyboard
like image 789
Angulandy2 Avatar asked Nov 28 '18 11:11

Angulandy2


People also ask

How do you blur input on enter?

If you want to fire blur event with key enter event, then simply pass $event in function and $event. target. blur(); in the function.

What is blur () method?

blur() method removes keyboard focus from the current element.

What is Onblur in angular?

In this article, we are going to see what is blur event in Angular 10 and how to use it. The blur event is triggered when an element loses its focus. Syntax: <input (blur)='functionName()'/>

What is Keyup event in angular?

(keyup): (keyup) is an Angular event binding to respond to any DOM event. It is a synchronous event that is triggered as the user is interacting with the text-based input controls. When a user presses and releases a key, the (keyup) event occurs.


1 Answers

html file

<input type="text"  #search (keyup.enter)="doSomething()">

.ts file

import { Component, ViewChild, ElementRef} from '@angular/core';

  @ViewChild('search') search: ElementRef;

  doSomething(): void {
    this.search.nativeElement.blur()
  }

working stackblitz

like image 188
Ebin Manuval Avatar answered Oct 09 '22 03:10

Ebin Manuval