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');" />

This entry was posted on Saturday, November 22nd, 2008 at 04:56 and is filed under jQuery. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.





43 Responses to “How to .reset() form with jQuery”

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()

DownsTream

Thanks…

easy solution but it work!!!

Bruno Soares

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.

Doctors have treated a few of these babies with Viagra and observed vast improvements in their ability to get oxygen-rich blood from the lungs to other parts of the body. viagra http://www.tatansa.fr

the function with dollar sign returns a jQuery object and not the dom element itself. The solution is mentioned above in other comments but i’d like to introduce a jQuery function “get” which returns the dom element itself;

$(“#”+form_id).get(0).reset();

@prabhakaran: Thanks! Great solution! :)

[...] From: http://simple.procoding.net/2008/11/22/how-to-reset-form-with-jquery/ May 7, 2012 | Posted in: JS | Tags: jQuery | No Comments » [...]

Tai Nguyen

Sheesh, youngsters nowadays. It’s like they’ve completely forgotten that jQuery is a framework on top of Javascript. Does anyone even know Javascript anymore? Apparently not.

Let me explain exactly what happens when you run $(“#myform”)[0].reset();

1) jQuery will get the DOM element based on the ID you gave it.
2) It wraps that DOM element in a jQuery object with a bunch of other junk
3) It then returns a jQuery object with the DOM element in the zero index position.
4) When you request the zero-index array, you receive the DOM element back again.
5) You run the reset() method which is a native Javascript method that only works on DOM elements.

Oh and by the way, jQuery has to execute about 50 lines of code and do a bunch of extra crap just to return that jQuery object. And what does it use to get that original DOM element? Plain old Javascript: elem = document.getElementById( match[2] )

So can you see how insanely stupid it is to ask jQuery to run through all that garbage when all you had to do in the first place was:

document.getElementById(“#myform”).reset();

Thank you for that enlightening writeup. If any reader needs to visit a proficient NYC Doctor, contact our clinic by calling us through our contact number 212-740-4600. We are affectionate in keeping our patients free from harm and healthy all throughout the year.

Wow. The .each() method is kinda overkill if you’re just targeting one element.
I’ve wrote a small blog post about the topic myself including examples of the most common pitfalls when trying to reset a jQuery form and the shortest code possible to reset a form. It can be found here:

http://forwebonly.com/how-to-reset-an-html-form-with-jquery/

nhat quan

alkahoma

Desert Fox

Try this

$(‘form’).trigger(‘reset’)

thanks you :)

Working out the root problem from which the root was taken.
Sarah Friar – Goldman Sachs. Approach it the same way as right wingers want to regulate a variety
of industries, we are looking for a system that relies on biomass for energy is greatly decreased.
En esos casos, el prezzo viagra relaja al joven y permite que deje sus temores a
un lado, explica el psic?

This is certainly attention-grabbing, You are a quite specialist blog writer. I’ve joined your current rss feed and look ahead of time to help in search of additional within your fantastic post. Furthermore, I’ve got shared your blog inside my social support systems

Hi my family member! I want to say that this post is awesome,
great written and come with approximately all vital infos.
I’d like to look more posts like this .

Thank you, I’ve just been searching for information approximately this subject for a while and yours is the greatest I have found out so far. However, what about the bottom line? Are you positive in regards to the source?|What i do not understood is in fact how you’re now not really a lot more neatly-liked than you might be right now. You’re very intelligent.

Great blog right here! Also your website rather a lot up very fast! What host are you the use of? Can I am getting your associate hyperlink on your host? I want my web site loaded up as quickly as yours lol

I enjoy the beneficial details people source to your articles. Let me book mark your blog post along with have a look at once again in this article often. I am just quite sure I will learn a lot of completely new information right here! Best of luck for the next!

Excellent items from you, man. I have consider your stuff prior to and you’re just extremely excellent. I really like what you’ve acquired right here, certainly like what you are saying and the best way wherein you say it. You are making it enjoyable and you continue to take care of to keep it wise. I can not wait to read far more from you. This is actually a tremendous site.

Amazon aims to focus on customer experience through offering
a wide array of products at low prices at the most convenient possible way.
Word of mouth programmes capture, interpret and measure a huge range of unprompted online conversations about a brand.
Marketing majors, and others entering the advertising industry, generally work in one of
two areas of advertising: sales or creative.

My weblog – online marketing and advertising tools

My pal advisable I’d personally like that web-site. He or she has been entirely appropriate. This particular publish really designed my working day.. Affiliate Site You can not consider only exactly how so much time period I had put together put in due to this information! Thanks a lot!

If you want to place a bid yourself, its exposed to has.

Purely submit the Contact Form regarding the website.

Feel free to surf to my blog :: business intelligence

I read a lot of interesting posts here. Probably you spend
a lot of time writing, i know how to save you a lot of time, there is an online tool that creates readable,
google friendly articles in seconds, just search in google – laranitas free content source

Good day! I know this is kinda off topic but I was wondering if you
knew where I could find a captcha plugin for my comment form?

I’m using the same blog platform as yours and I’m having trouble finding
one? Thanks a lot!

Thank you for the excellent writeup.. medicare maryland The idea in truth was a leisure bill them. View sophisticated so that you can considerably additional reasonable within you! Incidentally, exactly how should we communicate?

awesome cool usb gadgets toys…

simple » Blog Archive » How to .reset() form with jQuery…

Http://Elizabethdougherty.Com/2013/04/27/Bill-To-Label-Gmos-Introduced-At-The-Federal-Level-Elizabeth-Dougherty-Food-Nation-Radio-Network/…

simple » Blog Archive » How to .reset() form with jQuery…

Leave a Reply


required


required (will not be published)