Ravshan Abdurasulov
Ravshan Abdurasulov

Reputation: 547

Reset form after submitting via jQuery

I want to write simple a contact form which after submitting that should clean all fields and alert. I am beginner in Javascript also in programming. My code shows me error:

$().reset(); is not a function

<form method="POST" id="myForm" data-toggle="validator" action="email.php">
  <h2 class="section-heading">Свяжитесь с нами:</h2>
  <div class="form-group">
    <label for="exampleInputEmail1">Имя:</label>
    <input style="background:none;" id="firstName" name="firtname" class="form-control" placeholder="Имя" required>
    <p id="p1"></p>
  </div>
  <div class="form-group">
    <label for="exampleInputEmail1">Тема:</label>
    <input style="background:none;" id="subjectTheme" name="subject" class="form-control" placeholder="Тема" required>
    <p id="p2"></p>
  </div>   
  <div class="form-group">
    <label for="exampleInputEmail1">Электронная почта:</label>
    <input style="background:none;" type="email" id="email" name="email" type="email" class="form-control" placeholder="Электронная почта" required>
    <p class="help-block with-errors"></p>
  </div>
  <div class="form-group">
    <label>Сообщение:</label>
    <textarea style="background:none;" name="message" class="form-control" rows="3"></textarea>
  </div>
  <input type="submit" id="sendButton" class="btn btn-default"/>
</form>
$('#myForm').submit(function(e) {
   e.preventDefault();
   $.ajax({
        type: 'POST',
        url: 'email.php'
   });
   $('#myForm').reset();
   alert("Success!");

})

Upvotes: 1

Views: 46

Answers (2)

pumpkinzzz
pumpkinzzz

Reputation: 2967

It is a javascript function, not jQuery

$('#myForm')[0].reset();

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

reset() is a method on the form DOMElement, not the jQuery object, hence the error. To achieve what you need, any of the following methods will work:

// call reset on the DOMElement
$('#myForm')[0].reset();
$('#myForm').get(0).reset();

// trigger a reset event via the jQuery object
$('#myForm').trigger('reset');

Upvotes: 3

Related Questions