How can I find out which submit button was clicked in javascript?
function submitForm(){
//if find open popup
//else if add continue in the same window
}
<form action="/findNames" onsubmit="return submitForm()">
<input type="submit" value="Add" onclick="submitForm(this)"/>
<input type="submit" value="Find" onclick="submitForm(this)"/>
</form>
WHAT I HAVE TRIED:
I tried to do it with onclick instead as follows but when the "Find" button is clicked it opens a popup window and submits the parent window as well. How can I stop it from submitting the parent window?
function submitForm(submit){
if(submit.value=="Find"){
find();//opens a popup window
}else if(submit.value == "Add"){
//stay in the same window
}
}
function find(){
var width = 1010;
var height = 400;
var params = 'width='+width+', height='+height;
popupWin = window.open('find.do','windowname5', params);
popupWin.focus();
}
<form action="/findNames">
<input type="submit" value="Add" onclick="submitForm(this)"/>
<input type="submit" value="Find" onclick="submitForm(this)"/>
</form>
yes, multiple submit buttons can include in the html form. One simple example is given below.
Put a hidden field. And when one of the buttons are clicked before submitting, populate the value of hidden field with like say 1 when first button clicked and 2 if second one is clicked. and in submit page check for the value of this hidden field to determine which one is clicked. Save this answer.
It depends on your purpose. In your code, if you want to submit the form to your server, you should use "Submit". However, if you want to do something from client side by clicking the "Button", the input type "Button" is the choice.
Let's learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method 'post' and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.
An input
of type="submit"
will always submit the form. Use an input
of type="button"
instead to create a button that doesn't submit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With