This is my jQuery code:
$('#contactForm').submit(function(e){
e.preventDefault()
$('#loading').css("display", "block")
$.ajax({
type : "POST",
url : "/contact/",
data: {
sender_name : $('#name').val(),
sender_email : $('#email').val(),
message_subject : $('#subject').val(),
message_text : $('#message').val(),
csrfmiddlewaretoken : csrftoken,
datatype : "json",
},
success: function(){
$('#loading').css("display", "none"),
$('#sent-message').css("display", "block")
},
});
});
It works without any issue. However I try to reset it after submission, and after searching on StackOverflow I added this:
success: function(){
$('#loading').css("display", "none"),
$('#sent-message').css("display", "block"),
$('#contactForm').trigger('reset')
},
And this:
success: function(){
$('#loading').css("display", "none"),
$('#sent-message').css("display", "block"),
$('#contactForm')[0].reset()
},
And none of them works. What I'm missing?
$('#contactForm')[0].reset()works correctly. Note thatreset()will set the fields back to their initial values, and not empty values, so if you pre-populated the fields you will need to manually setval('')on them instead of resetting the entire form. If this still isn't working for you ensure that you have only 1#contactFormelement in the DOM and no errors in the console.reset()is not settings the fields back to their initial values, which is empty. I have only onecontactForm(the rest of jQuery code works without issue) and there is no error.successhandler being called?