Reputation: 29683
I have a form as below which I validate using this validator plugin:
<div class="col-md-8 col-md-offset-2 form-contact">
<form action="/Home/SendMessage" id="contact-form" method="post" autocomplete="off" class="wow bounceInUp has-validation-callback animated">
//..All other form controls
</form>
</div>
but when I tried to reset
the form
as below
$('form#contact-form').reset();
OR
$('#contact-form').reset();
it gave me console.error
Uncaught TypeError: $(...).reset is not a function
but when I try to reset
the form as below it resets perfectly.
$('.form-contact form').get(0).reset();
Can anyone tell why this is happening or what is the reason behind this?
Upvotes: 0
Views: 111
Reputation: 3194
It is because $('#contact-form')
retuns the form in an array of the object. In this case array of form object with length 1.
Write that in browser console and see what happens.
the reset()
is a native javascript function which requires specific form
object which the selector doesn't provide without $('#contact-form')[0]
use $('#contact-form')[0].reset();
which gets first form
from the array.
try console.log($("#contact-form")); in the console. then you can see the result by expanding the object.
Upvotes: 1