Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear all inputs, selects and also hidden fields in a form using jQuery?

I would like to clear all inputs, selects and also all hidden fields in a form. Using jQuery is an option if best suited.

What is the easiest way to do this... I mean easy to understand and maintain.

[EDIT]

The solution must not mess with check-boxes (the value must remain, but checked state must be cleared), nor the submit button.

What I am trying to do is a clear button, that clears all the options entered by the user explicitly, plus hidden-fields.

Thanks!

like image 550
Miguel Angelo Avatar asked Aug 29 '11 22:08

Miguel Angelo


People also ask

How do I clear all inputs in a form?

To clear all the input in an HTML form, use the <input> tag with the type attribute as reset.

How do you clear input value after submitting?

To clear an input field after submitting:Add a click event listener to a button. When the button is clicked, set the input field's value to an empty string. Setting the field's value to an empty string resets the input.


1 Answers

You can use the reset() method:

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

or without jQuery:

document.getElementById('myform').reset(); 

where myform is the id of the form containing the elements you want to be cleared.

You could also use the :input selector if the fields are not inside a form:

$(':input').val(''); 
like image 125
Darin Dimitrov Avatar answered Sep 23 '22 18:09

Darin Dimitrov