5

I have a form with 2 submit buttons. On the form tag, I put onsubmit="return foo();"

So when I click in one of the two submit buttons, I can retrieve the submit element thanks to document.activeElement in IE, Chrome, Firefox. But document.activeElement doesn't work in safari (document.activeElement return a body element instead of the submit button element).

Here my code (you can try it at https://jsfiddle.net/m6quo8rs/ ) :

<form onsubmit="foo();">

<input type="submit" id="submit_button_1" value="Submit button 1">
<input type="submit" id="submit_button_2" value="Submit button 2">

</form> 

<script type="text/javascript">

function foo()
{
    //Retrieve the submit button clicked thanks to document.activeElement :

    console.log(document.activeElement);
}

</script>

This code works in IE, Chrome, Firefox, but doesn't work in Safari.

Have you a solution for safari ?

6
  • 1
    document.activeElement is not supported in safari..you can bind the function with onclick on buttons and then use the target property to identify the button.
    – nikhil
    Commented Sep 18, 2016 at 13:53
  • Thanks, so I must remove onsubmit="return foo()" in the form tag ?
    – totoaussi
    Commented Sep 18, 2016 at 13:54
  • 1
    yes...no need of that if you will bind the onlick on button..
    – nikhil
    Commented Sep 18, 2016 at 13:55
  • Ok, but if I remove onsubmit="return foo();", how can I prevent submit action if the user didn't fill the form correctly ?
    – totoaussi
    Commented Sep 18, 2016 at 13:56
  • 1
    event.preventDefault();
    – nikhil
    Commented Sep 18, 2016 at 14:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.