$("...")[0].reset is not a function... resetting forms in jQuery

user2426162 picture user2426162 · May 27, 2013 · Viewed 36.8k times · Source

I'm trying to reset some forms with jQuery but I'm having some trouble. Aside from solutions that involve writing functions or custom plugins, I keep finding time and again that the reset method is not a standard part of jQuery but it is a part of standard Javascript.

Anyway, my first approach was to go with

$("#theForm").reset();

where the form's id="theForm".

That didn't work obviously, because it assumed that reset() was a part of jQuery.

The next thing I tried was

document.getElementById(theForm).reset();

Which didn't work either. I'm new to jQuery so I'm not sure if I can mix normal Javascript with jQuery. I must sound like a moron for saying this.

Anyway, After doing some more researching I found time and again that I could use reset() in jQuery by doing these...

$("#theForm")[0].reset();

or

$("#theForm").get(0).reset();

In every article that involves these two snippets everyone in the comments had gotten it working.

Except me.

The error console keeps telling me that what I have written does not exist. I have checked all of my code and there is no other instance of the word "reset", so it can't be that either.

Anyway, I'm stumped.

Answer

Sanjeev picture Sanjeev · Mar 26, 2014

Solution to original issue in this thread

For an HTML form like this:

<form id="theForm">
</form>

When using document.getElementById("theForm").reset(), if you are getting a js error saying

reset() is not a function

the reason might be an element which has reset as name or id. I faced same issue.
Naming any element as reset overrides reset() function.