21

I've read several posts, including this, and this, but I can't seem to get the input field to clear out after submitting. What is the most basic / elementary way to do this?

Currently I have this:

$(document).ready(function(){
    $('form[name=checkListForm]').on('submit', function() {
          // prevent the form from submitting
          event.preventDefault();
          var toAdd = $(this).find('input[name=checkListItem]').val();
          $('.list').append("<div class='item'>" + toAdd + "</div>")
          $('input[name=checkListItem').reset();
        });
});

My HTML is:

<!DOCTYPE html>
<html >
  <head>
    <meta charset="UTF-8">  
    <link rel="stylesheet" href="stylesheet.css">     
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script type="text/javascript" src="script.js"></script>
  </head>
  <body>
    <h2>To Do</h2>
    <form name="checkListForm">
      <input type="text" name="checkListItem" />
      <button type="submit" id="button">Add!</button>
    </form>
    <br/>
    <div class="list"></div>
  </body>
</html>
Community
  • 1
  • 1
TheRealFakeNews
  • 7,512
  • 16
  • 73
  • 114

5 Answers5

41

Instead of resetting, just set a blank value to the input field using this bit of code:

$('input[name=checkListItem').val('');

In order to reset all inputs in a particular form you could also use the following code (using the :input selector):

$('form :input').val('');
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
stark
  • 2,246
  • 2
  • 23
  • 35
  • Thanks, that worked. As a side question, do you know why the selector should be `input[name=checkListItem` and not `form[name=checkListForm`? – TheRealFakeNews Feb 18 '16 at 20:49
  • @AlanH: Because you want to set input's value to be blank, not form's. – Przemek Feb 18 '16 at 20:52
  • @AlanH : that's because the `input` selector basically selects a form control and it has a value that can be set, whereas ``$('form')`` basically means you're selecting an html element itself rather than a form control, since the `$('form')` does not have a value attribute unlike the `input ` selector, see my latest edit to the answer as well. – stark Feb 18 '16 at 20:59
  • When you use `element[attribute=value]` your filtering the elements/nodes - ie it selects all of type `element` then from those, only the one with `[attribute=value]` so `form[name]` find all **forms** with name attribute. If you did `form [name]` (ie with a space) then it would find all elements with name attribute inside the form. – freedomn-m Feb 18 '16 at 21:18
  • $('input[name=checkListItem]').val(''); you have missed ']' ;) – Pasindu Jayanath Oct 07 '18 at 07:21
  • Reset a form through the `reset()`. it's valid both for JavaScript and jQuery and clears all form elements besides inputs. – Jason Vasilev Jul 10 '19 at 09:40
2

This should work,(I've tested it myself).

$(document).ready(function(){
    $('form[name=checkListForm]').on('submit', function() {
          //other code
          $('input[name=checkListItem]').val("")
        });
});
kodvin
  • 1,236
  • 9
  • 14
1

This one worked for me; Few modification to. I use function(e) and e.preventDefault() and used $('input[name=checkListItem').val(''); instead of reset()

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>

    $(document).ready(function(){
        $('form[name=checkListForm]').on('submit', function(e) {
              // prevent the form from submitting
              e.preventDefault();
              var toAdd = $(this).find('input[name=checkListItem]').val();
              $('.list').append("<div class='item'>" + toAdd + "</div>")


              $('input[name=checkListItem').val('');
            });
    });
 </script>

<h2>To Do</h2>
<form name="checkListForm">
  <input type="text" name="checkListItem" />
  <button type="submit" id="button">Add!</button>
</form>
<br/>
<div class="list"></div>
Omari Victor Omosa
  • 2,814
  • 2
  • 24
  • 46
0

We can write after submission.

document.getElementById("myform").reset();
Sibeesh Venu
  • 18,755
  • 12
  • 103
  • 140
Siddharth Shukla
  • 1,063
  • 15
  • 14
0

Finally, this worked for me.

$(document).ready(function(){
$('#myForm').on('submit', function() {
$('#checkListItem').val('');
});
});
        
Osh
  • 1
  • 3