How to .reset() form with jQuery
Such jQuery call won’t work:
1 2 | $('#formId').reset(); // error: $("#formId").reset() is not a function |
As the tutorial shows (yes, I never read manuals before I get into troubles as well, but always google answers before think, lol), we can have the following:
1 2 3 4 5 | function resetForm(id) { $('#'+id).each(function(){ this.reset(); }); } |
…and somewhere on a page this:
1 | <input type="button" onclick="resetForm('formId');" /> |
Wha-ha! Just use
$(’#myFormId’)[0].reset();
“$(’#myFormId’)[0].reset();”
Thanks Konstantin, easy solution
this doesnt work..
someone please help…
add this at the end of the form
and try this,
$(’#rst_form’).click()
add this at the end of the form
<div style=”display=’none’” ><input type=”reset” id=”rst_form”></div>
and try this,
$(’#rst_form’).click()
Thanks…
easy solution but it work!!!
only this:
$(’form’)[0].reset();
get the form element…. and not the specific form directly: (’#myform’)
it work => $(’#myform’)[0].reset()
Thanks for the idea
Thanks a lot for a good idea.
well, it would better is everybody use all-around jQuery style, what means that correct solution is
$(’#myform’).get(0).reset();
My shot:
$(resetSelector).bind(’click’, function (event) {
event.preventDefault();
if(confirm(”Reset form?”)) {
$(this).closest(’form’).get(0).reset();
}
});
just place somewhere in form button will be invisible and not clicable but:
anywhere in you scrip you can use: $(”my-reset-button”),click()
I’m tested it today - it works !
brgds, Marek
It wasn’t working for me until i moved the reset command from after a .prepend to before it. I guess don’t change any of the page content before attempting the form reset.
Is not workink for me, what does it mean $(”my-reset-button”)?
Good article but I agree with Konstantin, you don’t need to create a function just to do this. Also, you should look into custom jQuery functions rather than using vanilla javascript for this. Just a thought.