5

Does Bootstrap offer anything for clearing form input fields via a button?

Or do I need to roll my own through jquery?

From this post jQuery Validate resetForm() doesn't reset the onFocus validation, there seems to be a resetForm() method coming from jquery but I always get a object doesn't support method when I try to access that method.

Community
  • 1
  • 1
4thSpace
  • 43,672
  • 97
  • 296
  • 475

3 Answers3

12

You can use reset method (this is DOM element method, it is not jQuery object method), like this

$('form').get(0).reset() // or $('form')[0].reset()
Oleksandr T.
  • 76,493
  • 17
  • 173
  • 144
5

Similar to other answers, if you want to reset the form (not necessarily clear it) you can use the following:

<button type="reset">

It's a little cleaner than embedding jQuery or javascript calls in the onclick...

Mir
  • 2,429
  • 1
  • 29
  • 34
  • To clarify - "Reset" means to put it back to the state it was in on initial page load. So for example if you had loaded some form data from the database or something in the back end... "Reset" will put it back to that initial data, where "Clear" would just make every field empty. – Mir Mar 24 '22 at 16:02
2

This worked for me (clearing the fields, not just resetting the form fields to inital value):

$(':input').val('');

but it seems to have side effects like submitting the form.

Alex P.
  • 1,140
  • 13
  • 27