/    Sign up×
Community /Pin to ProfileBookmark

capture button used to submit form

is there an easy way to capture the button object that was clicked to submit a for onSubmit? i’d like to capture it to apply changes onSubmit without having to hard code an id or name for the button if it’s possible? ?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@mrhooMar 05.2007 — It depends on how you call the onsubmit handler-

if you can call it from a validating function, you'll need to capture it there.

And if the form can be submitted by pressing enter, there won't be a reference to the button at all.

But you can get the target element (or srcElement) of any event that calls a function:

function(e){

var whosit;

if(window.event)whosit= event.srcElement; //IE

else if(e.target)whosit=e.target; //Everybody else

//whosit is element whose event called the function

}
Copy linkTweet thisAlerts:
@bcamp1973authorMar 05.2007 — crud, i completely spaced that issue. well, that at least gets me a partial solution, thank you! your fix leads me to another question. I commonly see function(e) and i don't quite understand it's use. can you elaborate?
Copy linkTweet thisAlerts:
@mrhooMar 05.2007 — Without any intervention, most browsers pass a parameter to an event handler that is basically the [B]this [/B]of the event.

IE doesn't, but every event generates a global window.event in IE,

so you can use window.event or event in IE to refer to the same event that the other browsers pass as an argument.
Copy linkTweet thisAlerts:
@bcamp1973authorMar 05.2007 — ahh, ok that makes more sense. thank you!
×

Success!

Help @bcamp1973 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...