Clear all forms on page load

23,493

Solution 1

You can try using the plain javascript reset method on a form

$('form').each(function() { this.reset() });

This should reset each form to its default state.

To re-enable all checkboxes, you can try:

$(':checkbox').prop('disabled', false);

Solution 2

maybe this is what your asking? not sure why you would need it. the fields should be blank on page load anyways. you should change the values php side.

$('input[type=text]').val('');
$('input[type=radio]').checked=false;
$('input[type=checkbox]').checked=false;

or maybe even

$("input:not(':button, :submit, :reset, :hidden')").val('').checked=false;
Share:
23,493
Tural Ali
Author by

Tural Ali

I'm a software architect with 10+ year of experience in various fields.

Updated on May 15, 2020

Comments

  • Tural Ali
    Tural Ali almost 4 years

    I want to clear all forms on page load. I tried to use this function on domready, but it doesn't help. I'm new to JavaScript. Is there anything wrong with this function?

       $(':input', form)
     .not(':button, :submit, :reset, :hidden')
     .val('')
     .removeAttr('checked')
     .removeAttr('selected');