22

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>
1
  • Single line of code to handle all the fields $(formselector)[0].reset(); Commented Dec 20, 2017 at 6:43

5 Answers 5

42

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('');
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, that worked. As a side question, do you know why the selector should be input[name=checkListItem and not form[name=checkListForm?
@AlanH: Because you want to set input's value to be blank, not form's.
@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.
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.
$('input[name=checkListItem]').val(''); you have missed ']' ;)
|
2

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

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

Comments

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>

Comments

0

We can write after submission.

document.getElementById("myform").reset();

1 Comment

This resets the entire form, not the field, as the OP asked.
0

Finally, this worked for me.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.