It will get the event on the target i hit, so it might be the Foo span og the Bar span or even the div itself.
I see that you use a ion-button, but what if I want my click to be on a custom element, containing different children, because it is not a default iOS/Android button.
Edit: misread your second post. Try importing ViewChild to your component and adding it to the specific element youâd like to access. A google search or a look at Ionic docs will show you how to capture the element with @ViewChild
<span #span1>Foo</span>
and in your function on component.ts
myFunction() {
console.log(this.span1);
// or whatever name you assign to it in component
}
Using ViewChild eliminates the necessity of using $event
It will be the div itself because the (click) event is binded to the div, not the child spans. However, the â$eventâ parameter will carry all the targetâs data (div) including its child elements (spans).
The ion-button was just an example, you can bind the click event on any other element as far as I know.
The target of the $event will be the spans if thatâs what you tapped on. If you want the div youâd have to backtrack using parents in $event. No it wonât be the div itself, unless thatâs what you click on. If you click on the span boxes it wonât be.
You right about the spans being possible targets, but you can tap on the div container without affecting the spans, in that case, the div will be the target. Also you wouldnât need to backtrack if the spans are the target. Like I said 8 months ago , the $event will carry all the data of the element itâs bound to including div { currentTarget } itself.
Yeah but like, users are gonna tap on the spans instead of the div at some point. We need a foolproof solution to work around it.
What I found elsewhere is event.Target.closest('div').* which seems to work well.
Okay, firstly the Event Objectâs target not Target's property doesnât have a closest() function, look it up. Wherever you âfoundâ that, it was supposed to be $(event.target).closest(). Itâs the JQuery HTMLElement that has that function. This isnât JQuery.
Secondly, the currentTarget here will ALWAYS be the div no matter how many elements are inside that div, no matter which one of those elements the user clicks/taps. You donât need to navigate the DOM.
Here, some docs on the JavaScript Event Object, read them and let this go okay.