Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Posts
    329
    Thanks
    85
    Thanked 8 Times in 8 Posts

    How can I get the element that caused an event to be dispatched?

    I am not referring to Event.target

    I know that Event.target points to the element that dispatched the event (e.g. I add an event listener to a form), but how can I determine which element was interacted with by the user to cause the form to dispatch the event? (e.g. the user clicks a button ... which causes the form to dispatch an event ... how can I determine which button was clicked?)

    EDIT: I know I can add an event listener to the button itself ... but I don't want to do that ... I want to add the event listener to the form and then determine what action within the form caused the event to be dispatched ... if that is possible?
    Last edited by XmisterIS; Oct 5th, 2018 at 10:14 AM.

  2. #2
    Regular Coder
    Join Date
    Apr 2011
    Posts
    190
    Thanks
    0
    Thanked 59 Times in 56 Posts
    Which event do you mean? When you listen for the click event on a form even.target points to the element which the user interacted it. When you listen for the submit event it points to the form element, but you shouldn't have more than one submit button on the same form anyway.

  3. #3
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,737
    Thanks
    26
    Thanked 590 Times in 583 Posts
    I know I can add an event listener to the button itself ... but I don't want to do that ... I want to add the event listener to the form and then determine what action within the form caused the event to be dispatched ... if that is possible?
    If you want to listen on the submit event - not possible (there is more than one possibility to trigger the submit, think of hitting Enter or by JavaScript). For click events you use Event.target.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  4. Users who have thanked Dormilich for this post:

    XmisterIS (Oct 5th, 2018)

  5. #4
    Regular Coder
    Join Date
    Jun 2010
    Posts
    329
    Thanks
    85
    Thanked 8 Times in 8 Posts
    Quote Originally Posted by Dormilich View Post
    there is more than one possibility to trigger the submit, think of hitting Enter or by JavaScript
    That's exactly it - I want a quick-and-easy way of gathering user behaviour data (i.e. how many click "submit", how many press enter, etc) that I can simply plug into my form's submit event listener. But if it's not possible, then, it's not possible! There are other ways. Thank you though.

  6. #5
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,737
    Thanks
    26
    Thanked 590 Times in 583 Posts
    i.e. how many click "submit", how many press enter, etc
    That is not relevant to the submit event. However, you can write event handlers for these cases.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •