-2

Let's say we have a code like this:

<script type="text/javascript">
        $(document).ready(function() {
           $("#signup").click(function(e) {
             e.preventDefault();
                var email = $("#email").val();
                var fname = $("#fname").val();
                $.post("signup.php", {email:email, fname:fname}, function(data) {
                 $("#result").html(data);
                });
                return false;
            });
        });
    </script> 

After clicking the submit button, we can get an error message if the fields are not properly filled, in that case, the form can still retain the values, but if "submission successful" message was returned, how do I conditionally reset the form fields

bodesam
  • 529
  • 4
  • 9
  • 24
  • move **var email = $("#email").val();....** inside post success callback – gaetanoM Jan 13 '19 at 14:33
  • 2
    Possible duplicate of [How to reset a form using jQuery with .reset() method](https://stackoverflow.com/questions/16452699/how-to-reset-a-form-using-jquery-with-reset-method) – Hp_issei Jan 13 '19 at 14:47
  • This is not a duplicate question,I'm aware of other similar questions, but this is quite difffrent, the reset IS CONDITIONAL based on value returned to the result div, not a blanket reset, I explained that much in my question. The form should only reset if the message is "submission succcessfull" and not something like "Your email adddress is invalid" – bodesam Jan 13 '19 at 14:59

1 Answers1

1

Add $(this)[0].reset(); after $("#result").html(data);

FZs
  • 16,581
  • 13
  • 41
  • 50