Click event target gives element or it's child, not parent element

Javascriptvue.jsVuejs2

Javascript Problem Overview


I've got this HTML element:

<div class="list-group">
    <a href="javascript:;" @click="showDetails(notification, $event)" class="list-group-item" v-for="notification in notifications" :key="notification.id">
        <h4 class="list-group-item-heading">{{ '{{ notification.title }}' }}</h4>
        <p class="list-group-item-text">{{ '{{ notification.created_at|moment }}' }}</p>
    </a>
</div>

And this Javascript:

return new Vue({
    methods: {
        showDetails: function (notification, event) {
          this.notification = notification

          console.info(event.target)
        }
    }
}

The problem is that event.target return the exact element I click. That means it can be the a element, or one of it's children (h4 or p).

How do I get the a element (the element with the @click handler), even if the user clicks on one of it's children?

Javascript Solutions


Solution 1 - Javascript

use event.currentTarget which points to the element that you attached the listener. It does not change as the event bubbles

https://developer.mozilla.org/en-US/docs/Web/API/Event/currentTarget

Solution 2 - Javascript

Alternative solution with CSS:

a * {
    pointer-events: none;
}

> The element is never the target of pointer events; however, pointer > events may target its descendant elements if those descendants have > pointer-events set to some other value. In these circumstances, > pointer events will trigger event listeners on this parent element as > appropriate on their way to/from the descendant during the event > capture/bubble phases.

Ref: https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events#Values

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStephan VierkantView Question on Stackoverflow
Solution 1 - Javascriptibrahim tanyalcinView Answer on Stackoverflow
Solution 2 - JavascriptItang SanjanaView Answer on Stackoverflow