Amit Pandey
Amit Pandey

Reputation: 314

Find who fired/triggered the event (Origin of triggered event)

I want to know if there is a way to find from where a event was triggered, i have a form which makes use of multiple JavaScript libraries and in particular case a submit is triggered which is not desired. i want to find the origin of that event. Is there a way to add a listener which records the origin of the event when a specific type of event is triggered.

Something like this or may be something else:

el.addEventListener('click', e => {
        console.log(e.getOrigin)
});

Edit:

just To make it clear, i know what e.target is, and no this is not what i am looking for

e.target gives the element on which that event was triggered, what i am looking for is - if the event was triggered from a function i want to know which function triggered it.

Upvotes: 1

Views: 2194

Answers (3)

Connor Low
Connor Low

Reputation: 7186

That's not something you are going to find in the language. If you are just trying to debug a particular situation, you might consider using jQuery audit (this is no longer available in the chrome web store, but if you really want, you can get it from the repo), or set an event listener breakpoint in the Sources tab of your Dev-tools.

Programatically, there is nothing you can do, unless you modify the existing libraries.

Upvotes: 3

Run_Script
Run_Script

Reputation: 2548

You've already got the correct structure, but instead of e.getOrigin you need to use e.target. From MDN docs:

The target property of the Event interface is a reference to the object onto which the event was dispatched

In your example, with a click event, this will return the element that was clicked by the user. Hopefully this is the behaviour you were expecting?

Upvotes: 0

ABGR
ABGR

Reputation: 5205

console.log(e.getOrigin) replace, e.getOrigin with e.target

Upvotes: 0

Related Questions