0

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?

6
  • $('#contactForm')[0].reset() works correctly. Note that reset() 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 set val('') on them instead of resetting the entire form. If this still isn't working for you ensure that you have only 1 #contactForm element in the DOM and no errors in the console. Commented Jul 1, 2021 at 13:13
  • @Sravani The OP already is Commented Jul 1, 2021 at 13:14
  • @RoryMcCrossan The form is not pre-populated. It's a contact form and reset() is not settings the fields back to their initial values, which is empty. I have only one contactForm (the rest of jQuery code works without issue) and there is no error. Commented Jul 1, 2021 at 13:16
  • @Sravani Did you read my question and code? Commented Jul 1, 2021 at 13:19
  • Is the success handler being called? Commented Jul 1, 2021 at 13:22

1 Answer 1

1

Please see my code snippet. Reset works property. If doesn't work then you need to update your html code here.

<form id="contactForm">
      <input type="text" />
      <input type="button" onClick="resetForm();" value="Reset">
    </form>
    <script
      src="https://code.jquery.com/jquery-3.6.0.min.js"
      integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
      crossorigin="anonymous"></script>
     <script>
      function resetForm()
      {
        $.ajax({
          url:"test.php",
          success:function(){
            $("#contactForm")[0].reset();    
          },
          error:function(){
            $("#contactForm")[0].reset();    
          }
          
        });
        
      }
     </script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.