clock  Mon - Sun 8.00 AM - 8.00 PM
fb
instagram
play store
pinterest

How to Reset or Clear a Form using javaScript

writter  Hariom Prajapati
Date  26 Jun 2022
Language  JavaScript
How to Reset or Clear a Form using javaScript

How to Reset or Clear a Form using javaScript

In this tutorial we will see how to reset or clear a form using JavaScript with .reset() method.

 

How to Reset or Clear a Form using javaScript

 

How to Reset or Clear a Form using javaScript

Follow below steps to reset or clear a form using JavaScript .reset() method.

 

Step 1 - Create a form with id="reset" and add input field inside <form> tag according to your need.

In first step you need to create a form with id="reset" (you can take any id name but make sure that same id name used inside step 3 script code where id called) input fields and submit button. 

 

Syntax -

<form id="reset">
<label> input name </label>
<input type=" " name=" "/>
<button type="submit" > Submit </button>
</form>

 

Step 2 - Create a input/button with type="button" and create a onclick function inside it.

In second step you need to create input/button with type="button" and also create onclick function.

 

Syntax -

<form>
<label> input name </label>
<input type=" " name=" "/>
<input type="button" onclick="newFunction()" value="reset"/>
<button type="submit" > Submit </button>
</form>


Step 3 -
 Now put below script code inside <body> tag.

In third step you need to insert Script code inside body tag.

 

Syntax -

<script>
function newFunction(){
document.getElementById("reset").reset() ;
}
</script>

 

Now you can reset or clear a form with reset button.

Below code is combination of all the above steps :-

 

For Example -

<!DOCTYPE html>
<html lang="en">

<head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- bootstrap cdn start -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
        integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
        integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous">
    </script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
        integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous">
    </script>
    <!-- bootstrap cdn end -->
    <!-- style start-->
    <style>
        .vertical-center {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }

        .input_field_class {
            border: 2px solid white;
            box-sizing: border-box;
            box-shadow: 5px 5px 5px 5px grey;
        }
    </style>
    <!-- style end -->
</head>

<body>

    <div class="container-fluid" style="overflow-x: hidden;">
        <div class="row vertical-center mt-4 mx-4">
            <!-- main form start -->
            <div
                style="width:600px;height:auto;border:10px solid white;box-sizing:border-box;box-shadow:5px 5px 5px 5px grey , grey 5px 5px 5px 5px inset;border-radius:150px 5px 150px 5px;margin-bottom:30px">
                <!-- form div start here -->
                <div class='mx-5 mb-2'>
                    <form action="" method="" id="reset">
                        <label for="full_name" style="font-weight: 600;" class="form-group mt-5">Full Name :</label>
                        <input type="text" name="full_name" id="full_name" placeholder="Full name"
                            class="form-control input_field_class" required> <br>
                        <label for="email" style="font-weight: 600;" class="form-group">Email Id :</label>
                        <input type="text" name="email" id="email" placeholder="Email Address"
                            class="form-control input_field_class" required><br>
                        <label for="dob" style="font-weight: 600;" class="form-group">Date Of Birth :</label>
                        <input type="date" name="dob" id="dob" placeholder="dob" class="form-control input_field_class"
                            required><br>
                        <label for="your_self" style="font-weight: 600;" class="form-group ">About Yourself :</label>
                        <textarea name="your_self" id="your_self" class="form-control input_field_class"></textarea><br>

                        <div class="d-flex justify-content-around mt-4 mb-3"><input type="button" class="btn btn-danger"
                                onclick="newFunction()" value="Reset"><button type="submit" id="btn"
                                class="btn btn-dark">Submit</button></div>
                    </form>
                </div>
                <!-- form div end here -->
            </div>
            <!-- main form end -->
        </div>
    </div>
    <!-- js start here -->
    <script>
        function newFunction() {
            document.getElementById("reset").reset();
        }
    </script>
</body>

</html>

 

Output

javascript

 

Comments 0

Leave a comment

Coursera, Codeacademy, Udacity, W3Schools, Udemy, Alison, TheNewBoston, edX, P.S.Codewars,Freecodecamp, Managing technical debt blog, Scrimba, Codepen, Codepen/challenges, The Odin Project, htmlreference.​io, cssreference.​io, Frontend Mentor, Dev Challenges, MDN, Code Mentor, Coding Dojo, CSS Battle, Codier, Ace Frontend, Can I Use, CSS Tricks, 30 Seconds of Code,tutorialspoint, Neumorphism, Shaddows Brumm, Fancy Border Radius, Glow Generator, Clothoid Corners, Glassmorphism, Clipy, CSS Filters, Base64 Image, Quantity Queries, Animations, Cubic-Bezier, Keyframes, Wait Animate, Transition.Style, graphic design, web design, website design, website builder, web developer, web designer, webdesign, ecommerce website, web design company, website creator, website designer, responsive web design, web development company, best website design, web design software, web page design, build a website, web developer salary, design website, web design courses, how to design a website, web design inspiration, website layout, web designer salary, web application development, ecommerce website design, web agency, software development company, web design tutorial, web programming, design company, website design templates, what is web designing, web developer jobs, website developer, web design agency, freelance web developer, web design services, freelance web designer, graphic design websites, web solutions, ecommerce website development, free website design, web development courses, webdev, web developers, web development tools, website design services, developpeur web, web design london, website design ideas, web designing and programming, design a website, web design and development, web dev, web development services, homepage design, best designed websites, cheap website design, learn web design, web design templates, web design tools, web design jobs, website design inspiration, web design india, flash website, website developers, designer websites, website services, website design cost, good website design, site design, simple website design, cool website designs, modern website design, graphic designer websites, webcode, best web design software, website making, free web design software, mobile website design, learn web development, front end web developer, how to become a web developer, web developer portfolio, web development company in india, python web development, web development tutorial, website company, website design and development, web company, webdesigning, professional website design, affordable web design, best web design company, creative web design, top website designs, website design pricing, web developer tools, how to develop a website