1

I am unable to get the value of the clicked button when two buttons share the same name but have a different value in a form.

function submit_form(form) {
  var status = form.status.value;
  alert(status);
}
<form onsubmit="return submit_form(this)">
  <button type="submit" name="status" value="1">Submit</button>
  <button type="submit" name="status" value="2">Save as Draft</button>
</form>

It will work if there is only one button.

Please take note that this operation is triggered during the form submission and not when the button is clicked.

The input element will work fine but I want the button to work since I need to declare two values if I use the input.

The element i used here is , not . Please!

Noel B.
  • 74
  • 8
  • It's odd you've tagged this with jQuery when you aren't using it at all, but see the duplicate I marked for details on how you can achieve this. Note that there's no built-in way to do it as the button click event and form submission event are entirely separate. – Rory McCrossan Aug 24 '18 at 08:09
  • 1
    you can use onclick event for buttons to append some attribute to form like and then check this attr value on submit – Arun Kumar Aug 24 '18 at 08:10
  • @RoryMcCrossan. This works perfectly in php. But I want javascript will do this tiime hoping that there is a simple way. Anyway, thank you! I will use the suggestion of Arun Kumar. – Noel B. Aug 24 '18 at 08:30
  • 1
    @NoelB. lol that's the exact method outlined in the duplicate I marked – Rory McCrossan Aug 24 '18 at 08:45

0 Answers0