0

I want to reset all the values of my inputs that I have within a form (textboxs, selects, checkboxs, radiobuttons,...) like if I reload the web, but when I touch a button.

$("#button").click(function () {
     //Here the code that I need   
});
Pablo De Luca
  • 795
  • 3
  • 15
  • 29

5 Answers5

8

You can reset a form using JavaScript built-in reset() function:

$("#otra").click(function () {
    $("#yourFormId")[0].reset();
});

Working demo:

$("#otra").click(function () {
  $("#yourFormId")[0].reset();
  return false; // prevent submitting
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form id="yourFormId">
  <input type="checkbox" name="myCB"/>
  <input type="text" name="myTB" value=""/>
  
  <button id="otra">Reset</button>
</form>
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
2

You can do it this way

$('input, textarea').val('');
$('select').find('option').prop("selected", false);
Hemal
  • 3,682
  • 1
  • 23
  • 54
1

you can reset the complete form using this code

$('#myForm').trigger("reset");
Prabu Guna
  • 344
  • 1
  • 3
  • 14
1

Try this:

$('form[name="myform"]')[0].reset();
Abhijith Sasikumar
  • 13,262
  • 4
  • 31
  • 45
0

My page preserves fields during post back, reset() doesn't remove them.

This helped me: https://stackoverflow.com/questions/6364289/clear-form-fields-with-jquery

$(".reset").click(function() {
    $(this).closest('form').find("input[type=text], textarea").val("");    
});

Other types can be added.

AJ AJ
  • 187
  • 2
  • 12