Submit event which button
Community Bot 1 1 1 silver badge. You might want to add a statement that resets the clicked attribute to false across the buttons in case the form submit is handled in an ajax way and you want to avoid getting previsouly clicked button again.
Oh, I see. You're adding your own "clicked" attribute. I was looking all over for a "clicked" boolean and couldn't find one anywhere. I never thought of making one myself. Good idea! Be aware that this only works with input elements, not button elements. This is unnecessary in Chrome with button elements. I simply handle the on submit event, construct a FormData object from the form, and it always includes the value of the button that was clicked.
FireFox does NOT exhibit this behavior, and instead doesn't send the button value at all. Chrome seems to be doing something like the above solution automatically, remembering which button was clicked and including it in the form data automatically. Of course, this lack of standard behavior plaguing everything in JavaScript anyway, so I'm not surprised , makes the feature useless.
Show 9 more comments. I found that this worked. Timo Tijhof 9, 6 6 gold badges 31 31 silver badges 46 46 bronze badges. Stan Stan 1, 7 7 silver badges 6 6 bronze badges. Sun Lin. Krinkle I coded it like that to show the most generic way possible.
I think devs are smart enough to optimize it to their tastes. Cleaner than the other solutions. Still haven't found a solution that works when pressing enter though — andrewtweber.
This answer will not work always. Actually you need the clicked button, not the focused as the focus action is not a click action. The selected answer of hunter is the correct one. I have a click event on submit buttons and submit events on their forms I used this to figure out the source of the event. In some browsers clicking the button you'll get the button as focused, in others you'll get nothing. In all browsers I tested, hitting enter while on an input keeps the input focused.
Show 1 more comment. For what it's worth, you don't need to use jQuery to get the id - it's even simpler to use pure Javascript: document. Note that this doesn't appear to work as expected on Safari.
In Safari document. In my case it's a modal div which contains the form. When the form is submitted: document. Nick F Nick F 8, 7 7 gold badges 69 69 silver badges 85 85 bronze badges. I do not know how about Safari but on Chrome worked good : Thanks! I had jquery unobtrusive validation running, and that intercepts before this and sets the invalid input as activeElement instead of the button.
Good point Daan: if you submit the form by hitting Enter there might not be a "button that was clicked. I've updated the answer to make this clear. Here's the approach that seems cleaner for my purposes.
Jonathan Camenisch Jonathan Camenisch 3, 1 1 gold badge 18 18 silver badges 9 9 bronze badges. What if this is an form submit event instead of click? Tony, sorry I didn't see your question sooner. This answer is entirely in regards to form submits. Submit buttons get clicked too, and thus they fire the click event. Upvoting this answer over hunter's answer since the form's submit events are fired before any child element click events.
This causes difficulties when your form's submit event is cancelled with return false , whereas this method works correctly since the click is bound to the form instead of its children. This fails to properly handle the case where the form is submitted by means other than a click e. When the form is submitted via other means, it is the first submit button that triggered the submit event, not the one last clicked. There is a semicolon missing after Thomas Williams Thomas Williams 1, 1 1 gold badge 13 13 silver badges 31 31 bronze badges.
Damn I have to see if my page works on Safari now doh. Why does Safari always have to be so difficult. I know that Firefox has evnt. But what you can do is add click handlers to each submit which will inform the submit handler as to which was clicked.
This solution automatically puts the name of the submit button pressed into the action hidden field. Both the javascript on the page and the server code can check the action hidden field value as needed. Create a Javascript Variable that will reference the button clicked. Lets call it buttonIndex. Now, you can access that value. OP stated he didn't want to modify the code for the buttons. This is the least-intrusive answer I could come up with using the other answers as a guide.
My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake. I like writing tutorials and tips that can help other developers. As well as demo example. View all posts by Admin. Your email address will not be published.
0コメント