Get the button that caused the submit from the form submit event

I’m trying to find the value of the submit button that triggered the form to submit, so in accordance I can do differents things, actually I use this:

$("#regform").on('submit', function(e) {
  if (($(document.activeElement).val()) == "cf") {
    e.preventDefault();
    // This doesnt work on iOs Devices
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="action.php" method="post" id="regform" class="regform" accept-charset="utf-8">
  <button class="btn btn-outline-primary" type="submit" id="check-cf" value="cf">Check CF</button>
  <input type="submit" name="submit" class="btn btn-success btn-lg btn-block" value="Reg">
</form>

document.activeElement doesn’t work in many cases. What should I use nowadays to be compatible to achieve that?

54 thoughts on “Get the button that caused the submit from the form submit event”

Leave a Comment