Solved

Clear form input field AJAX Success

Posted on 2016-07-30
1
36 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 500 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn how to dynamically set the form action using jQuery.
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)

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now