Can focusin
and focusout
be in one event? What is it called, then?
If not, is there a way to merge this in one function?
hide(e:any) {
$('.suggestion').hide();
}
show(e:any) {
$('.suggestion').show();
}
<section class="search-bar-wrapper" (focusout)="hide($event)" (focusin)="show($event)">
The main difference between this event and blur is that focusout bubbles while blur does not. The opposite of focusout is focusin .
The focusout event occurs when an element (or any elements inside it) loses focus. The focusout() method attaches a function to run when a focusout event occurs on the element, or any elements inside it. Unlike the blur() method, the focusout() method also triggers if any child elements lose focus.
The ng-focus directive tells AngularJS what to do when an HTML element gets focus. The ng-focus directive from AngularJS will not override the element's original onfocus event, both will be executed.
In Angular 8, event binding is used to handle the events raised by the user actions like button click, mouse movement, keystrokes, etc. When the DOM event happens at an element(e.g. click, keydown, keyup), it calls the specified method in the particular component.
First thing is you need to add tabindex
attribute to section
to make it to get focus event. Otherwise, it won't get focus event.
Focus event get triggered when an element is focus-able. Every time you click the element it is always get focused and we can remove the focus only on click outside the element. So, we can't remove focus on click
event of the same element.
focus
and focusout
both are different events we can't merge them
You can use *ngIf
also
<section
class="search-bar-wrapper"
tabindex="-1"
(focus)="show($event)"
(focusout)="hide($event)"
>
<div class="suggestion" *ngIf="canSee">
This is a suggestion
</div>
in the class of the component
casSee: boolean = false;
show(e: any) {
this.canSee = true;
}
hide(e: any) {
this.canSee = false;
}
You can use (focus)
and (focusout)
in Angular 2/4.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With