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
Related videos on Youtube
Author by
Christian Graf
Updated on June 04, 2022Comments
-
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.srcElement
in IE seems to be the target? Well, isn't there anything likeexplicitOriginaltarget
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 almost 11 yearspossible duplicate of Find the button object that called the event with jquery or javascript
-
apsillers almost 11 yearsBy "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 almost 11 yearsCan/are you willing to use JQuery? Could also be stackoverflow.com/questions/2351660/…
-
Christian Graf almost 11 yearsNo JQuery :( I've got one form with a submit button and some js functions, that call submit()
-
MaxPRafferty almost 11 yearsHow about stackoverflow.com/questions/831942/…
-
MaxPRafferty almost 11 yearsAre you bubbling events at all? Or do you want, say, the "clicked" element?
-
Christian Graf almost 11 yearsWhat is "to bubble events? I want the element, that has been clicked to submit the form.
-
m90 almost 11 yearsThe 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 almost 11 yearspossible duplicate of Crossbrowser equivalent of explicitOriginalTarget event parameter
-
Christian Graf almost 11 yearsYeah, but this question is more than 5 years old.
-
-
Christian Graf almost 11 yearsnope, the srcElement is -unfortunately- not the sender but the target :(
-
Wood almost 11 yearswhat about e.originalEvent || e.originalTarget; ?