Confirmation before submitting form using jQuery

Hans de Jong picture Hans de Jong · Apr 10, 2014 · Viewed 37.2k times · Source

I am trying add a confirmation before a form is submitted using jQuery. I found a nice example in another Stack Overflow question form confirm before submit but I can't get it to work.

jQuery:

$(function() {
    $('form#delete').submit(function() {
        var c = confirm("Click OK to continue?");
        return c;
    });
});

Template:

<form id="delete" action="{% url 'item_delete' item.id %}" method="post">{% csrf_token %}
    <input class="floatright" type="submit" value="Delete" />
</form>

How can we achieve this?

Answer

iHadaj picture iHadaj · Apr 10, 2014
   $('#delete').submit(function(event){
     if(!confirm("some text")){
        event.preventDefault();
      }
    });