Getting the sender of an event?

18,350

Here you have a function. You just need to evaluate wich parameter is present in your event object

function getTarget(e){
  e=e||window.event;
  return (e.target||e.srcElement);
};

See: jQuery - how to determine which link was clicked

Share:
18,350

Related videos on Youtube

Christian Graf
Author by

Christian Graf

Updated on June 04, 2022

Comments

  • Christian Graf
    Christian Graf almost 2 years

    How can I get the sender of an onSubmit event in any browser? Or at least in FF and IE? Esp. as event.srcElementin IE seems to be the target? Well, isn't there anything like explicitOriginaltarget in browsers other than FF?

    I'm looking for any solution in pure javascript, no JQuery.

    What I want to do: I've got a form. And depending on the sender I want to do differnt actions in the onSubmit(event) routine.

    What I got in my init function:

    var top_most_form = document.getElementById("upper_menu_form");
    top_most_form.onsubmit=function(event){
    
    var target = <apparently magical things have to happen here>;
    
    if ("upper_menu_form" == target.id) {
      AddSpinner();
      AddStarttimeToForm();
      AddThruputToUpperForm();
    } else {
      return false;
    }
    
    • Diodeus - James MacFarlane
      Diodeus - James MacFarlane almost 11 years
    • apsillers
      apsillers almost 11 years
      By "depending on the sender", do you mean you have multiple forms with a shared handler function, or you have one form with multiple submit buttons?
    • MaxPRafferty
      MaxPRafferty almost 11 years
      Can/are you willing to use JQuery? Could also be stackoverflow.com/questions/2351660/…
    • Christian Graf
      Christian Graf almost 11 years
      No JQuery :( I've got one form with a submit button and some js functions, that call submit()
    • MaxPRafferty
      MaxPRafferty almost 11 years
    • MaxPRafferty
      MaxPRafferty almost 11 years
      Are you bubbling events at all? Or do you want, say, the "clicked" element?
    • Christian Graf
      Christian Graf almost 11 years
      What is "to bubble events? I want the element, that has been clicked to submit the form.
    • m90
      m90 almost 11 years
      The submit event will be fired by the form element itself, so you'll have a hard time finding the input that triggered it. Listening for clicks and then triggering the submit yourself should work though.
    • m90
      m90 almost 11 years
    • Christian Graf
      Christian Graf almost 11 years
      Yeah, but this question is more than 5 years old.
  • Christian Graf
    Christian Graf almost 11 years
    nope, the srcElement is -unfortunately- not the sender but the target :(
  • Wood
    Wood almost 11 years
    what about e.originalEvent || e.originalTarget; ?