Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify which button submitted a form

Given the following form:

<form action method="post" onsubmit="func(this)">
    <button type="submit" value="prev"  >prev</button>
    <button type="submit" value="next"  >next</button>
    <button type="submit" value="submit">submit</button>
    <button type="reset"  value="reset" >reset</button>
</form>

Within func(), how to identify which button submitted the form?

like image 684
ashu Avatar asked Jun 11 '26 12:06

ashu


1 Answers

You are doing it wrong. You can attach click event to each button and take action. This is cleanest way.

<button onclick="alert('You are clicking on me');">Reset</button>

Instead of alert insert your custom function. You can make it more robust by not using inline javascript. In those functions you can choose if you want to submit form or not by preventing or passing default event. Like event.preventDefault()

like image 162
Mateusz Avatar answered Jun 14 '26 01:06

Mateusz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!