Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery to clear form fields after submit for text and checkbox

Tags:

jquery

I want to clear the form fields (form fields has text and checkbox) after submit. To clear the form, I have created a button. And there is separate button for submit.

<input type="reset" name="clearform" id="clearform" value="Clear Form" />

<form id="submit" method="POST" action="">

I have written jQuery code but its not working:

jQuery("#clearform").click(function(){
    jQuery("#submit input[type='text'], input[type='checkbox']").each(function() {
        this.value = '';
    });
});
like image 705
user2201935 Avatar asked Oct 21 '14 05:10

user2201935


People also ask

How do I empty a form field after submitting?

To clear all form fields after submitting:Add a submit event listener on the form element. When the form is submitted, call the reset() method on the form. The reset method restores the values of the input fields to their default state.

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.


3 Answers

Try this:

$('#clearform').on('click', function () {
    $('#form_id').find('input:text').val(''); 
    $('input:checkbox').removeAttr('checked');
});

One will clear all text inputs. Second will help unchecking checkboxes.

Hope it helps.

like image 57
MysticMagicϡ Avatar answered Nov 11 '22 17:11

MysticMagicϡ


There is a simple solution to reset form via jQuery:

$("form").trigger("reset");
like image 24
Faizan Noor Avatar answered Nov 11 '22 17:11

Faizan Noor


try the code shown below to reset a form

$('#submit')[0].reset();
like image 26
abdullacm Avatar answered Nov 11 '22 18:11

abdullacm