How to use onBlur event on Angular2?

Use (eventName) while binding event to DOM, basically () is used for event binding. Also, use ngModel to get two-way binding for myModel variable. Markup <input type=”text” [(ngModel)]=”myModel” (blur)=”onBlurMethod()”> Code export class AppComponent { myModel: any; constructor(){ this.myModel=”123″; } onBlurMethod(){ alert(this.myModel) } } Demo Alternative 1 <input type=”text” [ngModel]=”myModel” (ngModelChange)=”myModel=$event”> Alternative 2 (not preferable) <input … Read more

blur event.relatedTarget returns null

Short answer: add tabindex=”0″ attribute to an element that should appear in event.relatedTarget. Explanation: event.relatedTarget contains an element that gained focus. And the problem is that your specific div can’t gain a focus because browser thinks that this element is not a button/field or some kind of a control element. Here are the elements that … Read more

jQuery: fire click() before blur() event

Solution 1 Listen to mousedown instead of click. The mousedown and blur events occur one after another when you press the mouse button, but click only occurs when you release it. Solution 2 You can preventDefault() in mousedown to block the dropdown from stealing focus. The slight advantage is that the value will be selected … Read more