Skip to content Skip to sidebar Skip to footer

How To Use Onsubmit() To Show A Confirmation If There Are Multiple Submit Buttons On The Same Form?

> <inputtype='submit' name='undo' value='Undo' onclick="return confirm('Are you sure you want to rollback deletion of candidate table?')"/> <inputtype='submit' name='no' value='No' onclick="return confirm('Are you sure you want to commit delete and go back?')"/> </form>

Worked fine. just changed onsubmit() to onclick(). as the function of both in this situation is same.

Solution 2:

You could bind to onclick instead of onsubmit - see below.

<script>functionsubmitForm() {
    returnconfirm('Rollback deletion of candidate table?');
}
<script>

<form><inputtype='submit'onclick='submitForm()'name='delete'value='Undo' /><inputtype='submit'onclick='submitForm()'name='no'value='No' /></form>

Or alternately, using jQuery:

<script> 
$(document).ready(function() {
    $('form input[type=submit]').click(function() {
        returnconfirm('Rollback deletion of candidate table?');
    });
});
<script>

Solution 3:

<form onsubmit="submitFunction();">
    <inputtype='submit' name='delete' value='Undo' />
    <inputtype='button' onclick="declineFunction()" name='no' value='No' />
</form>

I wouldnt try to create a submit but rather just a button that has a onclick="function()" and then use javascript to set a variable to see how many times they have clicked it and a alert();

hope this helps :D

Solution 4:

Here's an event-listener based solution that avoids inline event handlers (useful if your site has a Content Security Policy that forbids inline JavaScript):

HTML:

<form method="post">
    <input type="submit"id="deleteButton" name="delete" value="Undo" />
    <input type="submit"id="noButton" name="no" value="No" />
</form>

JS:

document.getElementById("deleteButton").addEventListener("click", function(evt) {
    if (!confirm("Are you sure you want to rollback deletion of candidate table?")) { 
        evt.preventDefault();
    }
});

document.getElementById("noButton").addEventListener("click", function(evt) {
    if (!confirm("Are you sure you want to commit the transaction?")) { 
        evt.preventDefault();
    }
});

Solution 5:

Just use two of the same form. One for each button:

<formonsubmit="return confirm('Are you sure you want to rollback deletion of candidate table?')"><inputtype='submit'name='delete'value='Undo' /></from><formonsubmit="return confirm('Are you sure you want to rollback deletion of candidate table?')"><inputtype='submit'name='no'value='No' /></from>

Also, if you would done your research you would find these:

Post a Comment for "How To Use Onsubmit() To Show A Confirmation If There Are Multiple Submit Buttons On The Same Form?"