Onclick event for button

I have two buttons, save button and delete button. I need to add onclick event to both of the button. The onclick event function for delete button is working but I don’t know how to onclick event to my save button.

This is my code.

<div class="form-group last">
<div class="col-sm-offset-3 col-sm-9">
    <input class="btn btn-info" id="savebutton" type="submit" name="save"  value="Update" > 
    <input class="btn btn-default" id="delete" type="submit" name="delete" value="Delete" onclick="return confirm('Are you sure to delete?');"> 
</div>

What I want to do is when I click the save button, the record will be save into database and something alert box will display and if I click ‘OK’, it will go back to the previous page.

don’t make a handler that does both. make a handler for each case an attach it to the appropriate button.

can I know how?

see http://api.jquery.com/on/

Check out this - https://www.w3schools.com/jsref/event_onclick.asp - if you use vanilla js
And this https://api.jquery.com/click/ if jquery

Delete? You mean reset the form?
Use this:

<input type="reset" value="Reset">

for your eventhandlers, I would not attach a click event to the submit button. There are other ways of submitting a form (using the enter key for instance). You would attach an event handler to your <form> and listen for submit.

In your HTML, the <form> element is not visible. It would be helpful to paste the HTML with the form element. Or if you have no form element yet, add one.

document.querySelector('#yourForm').addEventListener('submit', function(event){
    if (!confirm('are you sure?')) {
        // abort submission, if not confirmed
        event.preventDefault()
    }
})

Same for the reset/delete button:

document.querySelector('#delete').addEventListener('click', function(event){
    if (!confirm('are you sure?')) {
        // abort submission, if not confirmed
        event.preventDefault()
    }
})

I prepared a little codepen for you to experiment with.

Hope this helps.

Best,
Martin

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.