I am using like this in my HTML file:
<div class="component" >
<div class="component-header" >
<a href="javascript:" (click)="onClick($event)" >
<i class="fa fa-chevron-right"></i>
<i class="fa fa-chevron-left"></i>
</a>
</div>
</div>
And in my JS file getting the class as:
onClick(event) {
var target = event.target;
var pElement = target.parentElement.parentElement;
var pclassAttr = pElement.attributes.class;
console.log(pclassAttr);
}
Actually some time it consider that I’m clicking on the anchor and console class is "component" and some times it consider that I’m clicking on the "fa fa-chevron-right" and console class "component-header".
I need always console class as "component" when click on anchor.
Is there any thing wrong please suggest.
Just use currentTarget
instead of target
. It will always be your anchor element because
currentTarget always refers to the element to which the event handler has been attached:
onClick(event) {
var target = event.currentTarget;
var pElement = target.parentElement.parentElement;
var pclassAttr = pElement.attributes.class;
console.log(pclassAttr);
}
See also
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