1

I have been trying to use jQuery to reset the form

$('#check_in_form')[0].reset();

Does not work. Using trigger() does not work too

$('#check_in_form').trigger("reset");

When I use these 2 methods, I get this error on my browser console

TypeError: $(...)[0].reset is not a function

Using plain Javascript does not work too.

getElementById("check_in_form").reset();

I finally have to resort to 'clicking' the Reset button

$('#reset').click();

What could be the problem here? jQuery is working on this page because I depend heavily on it for other portions of the page.

4
  • Can you make a fiddle? trigger('reset') should be the way to do it. The only way I'd see it not working is if $('#check_in_form') doesn't return anything. Using plain jquery doesn't work because reset is a jquery method Commented Jun 16, 2015 at 23:58
  • 1
    Works here jsfiddle.net/jvbqt8yv. Where's the rest of your code? Commented Jun 16, 2015 at 23:59
  • make sure check_in_form is form id, not reset button id. Commented Jun 17, 2015 at 0:02
  • Thanks @PhilVarg , user86745458 . Web page broken into many distinct files and too much work to sanitize content. PeterKA 's answer nailed it. Why people downvote instead of asking clarifying questions? I don't care for reputation points, but this will discourage new users. Commented Jun 17, 2015 at 1:58

2 Answers 2

6

Quite likely you have:

<input type="reset" name="reset" id="reset"/>

The problem is with name="reset". In JS form.reset refers to this DOM element and therefore causes a name conflict with the reset() method.

Therefore form.reset() ----> TypeError: $(...)[0].reset is not a function means just that.

$('#reset').on('click', function() {
    $('form')[0].reset();
});
input[name=reset] {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input type="radio" name="gender" value="A"/> A
  <input type="radio" name="gender" value="B"/> B
  <input type="radio" name="gender" value="C"/> C
  <input type="reset" name="reset"/>
  <button id="reset">Click To Reset</button>
</form>

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @PeterKA . That was exactly the problem. I don't understand why you mark my question as duplicate. I have looked at all questions using jQuery to reset HTML form in stackoverflow, MDN and jQuery docs before posting today. No one was silly enough to name button id="reset". This question may be useful for someone having the exact problem
@hanxue you're right. The only other question with the same error did not seem to have any good answer: stackoverflow.com/questions/16780005/…. I've voted to reopen the question.
Just to clarify, the issue is with the name attribute, not the id attribute?
I set both name and id to reset initially. Changed both to reset_button to solve the problem.
Thanks. As hanxue predicted, this exact issue was causing my problem and PeterKA's answer helped identify the problem. In my case, I'm not creating the DOM, I'm making a userscript, so I guess I'll either have to resort to using .click() on the input or using .setAttribute() to alter the name attribute (though probably not, since it might mess other stuff up)?
2

Here's working JS for you. Both vanilla js and JQuery.

https://jsfiddle.net/scheda/5e0h3wxc/

//vanilla JS
var button = document.querySelector('#form1-button');
button.addEventListener('click', function(e) {
    e.preventDefault(); 
    document.querySelector('#form1').reset();
});

//JQuery
$('#form2-button').click(function(e) {
    e.preventDefault();
    $('#form2')[0].reset();
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.