I want to reset a form in a JQuery click event function. How to do that ?
Asked
Active
Viewed 3,095 times
5 Answers
14
At it's simplest: $("form-selector-here")[0].reset()
but also see: Resetting a multi-stage form with jQuery
Note that jQuery isn't necessary for this at all, as $(selector)[0]
obtains the original DOM element. You could also say document.getElementById("myFormId").reset()
.
$("#btn1").click(function(){
$("#form1")[0].reset();
// OR
document.getElementById("form1").reset();
});
-
1The accepted answer to that question is just setting the input fields to blank, but a true reset sets all input fields to their original value. – David Hedlund Sep 07 '12 at 07:07
-
I think the answers to that question collectively cover the important scenarios, though? – Tim M. Sep 07 '12 at 07:09
0
you could do it like this:
$(function(){
$('#resetForm').click(function(){
$('form input[type=text]').val('');});
});

Dirty-flow
- 2,306
- 11
- 30
- 49

mattematico
- 669
- 3
- 14