Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Clear form input field AJAX Success

Posted on 2016-07-30
1
Medium Priority
?
160 Views
Last Modified: 2016-08-09
I would like to clear the email field of my form after the AJAX call returns success.  However, I can't seem to make it work.  Any suggestions will be appreciated:

 <script>
                                  var email = $('#email');
                        $(function () {
                            $("#newsjoin").on('submit', function (e) {
                                e.preventDefault();
                            });
                            $("#submit").click(function () {
                                $.ajax({
                                    url: 'http://MYSITE.com/includes/functions.php',
                                    data: $("#newsjoin").serialize(),
                                    method: 'get',
                                    success: function (d) {
                                        if (d.length) {
                                            // there are no errors
                                            $( "#successmessage").html(function() {
 var successmessage = d;
  return successmessage;
// CLEAR INPUT HERE???
});
                                        } 
                                    }
                                });
                            });
                        });
                    </script>
<html>
<form method="post" id="newsjoin" method="post"  on>
<input name="email" type="text" id="email" class="newsletter-input"/>
<input name="submit" type="image" id="submit" src="img/buttons/submit.png" alt="Join Mailing List" />
</form>

Open in new window

0
Comment
Question by:rgranlund
1 Comment
 
LVL 4

Accepted Solution

by:
Branislav Borojevic earned 2000 total points
ID: 41736042
You should include this in your script:

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

Open in new window


or in plain javascript:

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

Open in new window


That should do it.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question