All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Reset Form In jQuery

Last Updated : Mar 11, 2024

How To Reset Form In jQuery

In this article we will show you the solution of how to reset form in jQuery, here we needs to use reset and trigger() method for achieve the result. The trigger() method triggers the specified event and the default behavior of an event for the selected elements.

Passing reset statement on trigger method which is resetting form fields just by simple click on reset button when triggered by users on web page.

Step By Step Guide On How To Reset Form In jQuery :-

At html block, we defined form ‘frm’ with three input elements for collects name, mail id from users, submit button and lastly button ‘Reset’.

In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser and within that we waiting for click event occurrence which is appended with button.

Here we specifying form ‘frm’ and appended with trigger() method there we passing ‘reset’ property for triggers reset.

<!DOCTYPE html>
<html>
    <head>
        <title>Reset Form</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $("#rs").click(function(){
                    $("#frm").trigger("reset");
                })
            });
        </script>
    <body>
        <form id="frm">
            <input type="text" class="nm" placeholder="Enter Name">
            <input type="text" class="em" placeholder="Enter Email">
            <input type="submit" value="Submit">
        </form>
        <input type="button" id="rs" value="Reset">
    </body>
    </head>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In script we defined ready() method within that we defined click() method which is appends with button by using id ‘rs’ for generates click event when user clicks on ‘Reset’ button.
  8. Within function we specified form element by id ‘#frm’ for points form, then we needs to append this with trigger() method with ‘reset’ property for resets the form when user clicks on reset button.
  9. In html block we defined form with id ‘frm’ and three input elements ‘Name,Email’, submit button for collect inputs from user and button ‘Reset’ with id ‘rs’.
  10. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to reset form in jquery.

Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes without error.

When we executes program on browser we can see two empty text boxes and button ‘Submit’, ‘Reset’ now user needs to fill text box then clicks on reset button.

Then we can see form elements are resets like refreshing all input fields just by one click.

I hope this article on how to reset form in jQuery helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪