Html – Clear form fields with jQuery


I want to clear all input and textarea fields in a form. It works like the following when using an input button with the reset class:

$(".reset").bind("click", function() {
  $("input[type=text], textarea").val("");

This will clear all fields on the page, not just the ones from the form. How would my selector look like for just the form the actual reset button lives in?

Best Solution

For jQuery 1.6+:

  .not(':button, :submit, :reset, :hidden')
  .prop('checked', false)
  .prop('selected', false);

For jQuery < 1.6:

  .not(':button, :submit, :reset, :hidden')

Please see this post: Resetting a multi-stage form with jQuery



As jQuery suggests:

To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method.