5

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;
}
Christian Graf
  • 406
  • 2
  • 10
  • 26
  • possible duplicate of [Find the button object that called the event with jquery or javascript](http://stackoverflow.com/questions/13887252/find-the-button-object-that-called-the-event-with-jquery-or-javascript) – Diodeus - James MacFarlane Jul 25 '13 at 16:02
  • 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? – apsillers Jul 25 '13 at 16:03
  • Can/are you willing to use JQuery? Could also be http://stackoverflow.com/questions/2351660/javascript-how-to-get-value-of-sender-input-element-from-keyup-event – MaxPRafferty Jul 25 '13 at 16:04
  • No JQuery :( I've got one form with a submit button and some js functions, that call submit() – Christian Graf Jul 25 '13 at 16:05
  • 1
    How about http://stackoverflow.com/questions/831942/can-i-find-out-from-a-javascript-method-who-which-dom-element-called-me – MaxPRafferty Jul 25 '13 at 16:06
  • Are you bubbling events at all? Or do you want, say, the "clicked" element? – MaxPRafferty Jul 25 '13 at 16:07
  • What is "to bubble events? I want the element, that has been clicked to submit the form. – Christian Graf Jul 25 '13 at 16:19
  • 1
    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 Jul 25 '13 at 16:30
  • possible duplicate of [Crossbrowser equivalent of explicitOriginalTarget event parameter](http://stackoverflow.com/questions/179826/crossbrowser-equivalent-of-explicitoriginaltarget-event-parameter) – m90 Jul 25 '13 at 20:09
  • Yeah, but this question is more than 5 years old. – Christian Graf Jul 26 '13 at 07:40

1 Answers1

7

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

Community
  • 1
  • 1
Wood
  • 1,766
  • 1
  • 12
  • 10