Been researching this concept for a while now, and still not certain on best approach.
I'd like to capture a click event and trigger any (default or manually attached) events that follow it at a later time. More specifically, I want to record a tracking event when an element in the .container
is clicked. After it's been recorded, I want to resume the behaviors of the clicked element. Is this functionality attainable in this manner?
<div class="container">
<span>My Text</span>
<a href="/myurl">My Link</a>
<button type="submit">My Button</button>
</div>
I'm adding a click event to the element (whether it has others already or not), and capturing and preventing it's default behavior:
elementInsideContainer.addEventListener('click',e => {
e.preventDefault();
// submit some click tracking data via ajax
// carry on with other events on element
});
I know I have access to srcElement.href
and could window.location
the user there when capturing a click event on an <a>
tag, but could that really be the best way to do this? What if the element is a type=submit
button or just a <span>CLICK ME!</span>
that I want to track (as shown above)? Been reading about dispatchEvent(event)
but not sure if it's what I'm looking for.