Control Freak
Control Freak

Reputation: 13233

.reset() function causing error instead of executing

For some reason I'm having trouble coding in to the reset button. I have this code:

 <form name="sub">
   ...
   <button type=submit>Submit</button>
   <button type=reset>Reset</button>
 </form>

When I click reset, it reset's the form, as usual. However, when I try to bind a manual reset option on the button, like this code, it returns an error:

 var $f = $("form[name=sub]");

 $f.find("button[type=reset]").click(function(e){
   e.preventDefault();
   $f.reset(); // <-- Uncaught TypeError: undefined is not a function
   $(window).scrollTop(0);
 });

I get this error: Uncaught TypeError: undefined is not a function

Tried using document.form.sub.reset(); and tried $f = (global variable) instead of var $f = that doesn't work either. What's going on ?

Upvotes: 1

Views: 1524

Answers (2)

Alex
Alex

Reputation: 11245

Then you try DOM 0 via document.form.sub.reset(); you have misprint:

document.forms.sub.reset();

You can use it in jQuery, just get access to node:

 $("form[name=sub]").find("button[type=reset]").click(function(e){
   e.preventDefault();
   $f.get(0).reset();//jQuery.fn.get return node, not jQuery object
   $(window).scrollTop(0);
 });

Upvotes: 3

MayThrow
MayThrow

Reputation: 2201

reset() is a DOM method so use it on DOM object $f[0]

Upvotes: 7

Related Questions