0

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.

HoldOffHunger
  • 10,963
  • 6
  • 53
  • 100
Amit Pandey
  • 204
  • 1
  • 12
  • 1
    `event.target` (or `e.target` for your code) gives the element that triggered the event, is that what you want? What do you mean by "origin"? – HoldOffHunger Jun 05 '20 at 14:18
  • No, 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. – Amit Pandey Jun 05 '20 at 14:21
  • @AmitPandey What do you mean by *the event was triggered from a function*? How can a function trigger an event? – Run_Script Jun 05 '20 at 14:24
  • i have some libraries, which add manual event triggers to my elements, something like element.dispatchEvent('click') or trigger() in jQuery . i want to find that if a click event was triggered it was which function or which piece of code, like from function x or function y. – Amit Pandey Jun 05 '20 at 14:28

3 Answers3

2

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, 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.

Connor Low
  • 1,771
  • 13
  • 22
0

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

ABGR
  • 3,606
  • 1
  • 15
  • 35
0

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?

Run_Script
  • 2,146
  • 2
  • 10
  • 24