×

Please Login or Register to continue.

3
(1.8K Views)

A website cannot be completed without a form. I am creating a form where I want to create a reset button. The reset button should be used to clear or remove all text boxes, and textarea values. I want like when the user to clicks the reset button then form fields should be reset (clear or remove all values from the text boxes, text area, etc) 

HTML code - 

<!DOCTYPE html>
<html>
<head>
   <title>Clear all form fields values on reset button click in jQuery</title>
</head>
<body>
<form>
   Name: 
   <input type="text" name="name" id="name">
   Address: 
   <textarea name="address" id="address"></textarea>
   <button type="button" name="reset_btn" id="reset_btn">Reset</button>
</form>
</body>
</html>

Clear all form fields values on the reset button and click on jQuery

I have two fields in my form, name, and address. I want to remove or clear these field values on the reset button click. The form values should be clear. I have created a reset button in my HTML form. 

How can I reset form field values on the button click in jquery?

(4.3K Points)
in jquery

Share

2 Answers
(3.4K Points)
1

JavaScript is very complex for writing codes that's why we use jQuery to write less and do more without any issues. If you want to reset form fields on the reset button like a clear text box or Textarea values from the form then you should use the jQuery val() method. I know we use val() method to save the value from the field id to the variable but there is a change in the val() method. You can reset (clear,remove) form fields value using val(' ') method only. 

How to reset form fields on a button click in jQuery? 

You can reset form fields on a button click-

1. First create a form with input boxes. 

2. Create a reset button in the same form. 

3. Use jQuery CDN library inside the <head> tag. 

4. Write scripts. 

5. Use the jQuery ready() function and click() method. 

6. Reset value using field id and val() method. 

Let's create an example- 

<!DOCTYPE html>
<html>
<head>
   <title>Reset form fields in jQuery</title>
</head>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#reset_btn").click(function(){
        $("#name").val(''); 
        $("#address").val(''); 
     });
});
</script>
<body>
<form>
   Name: 
   <input type="text" name="name" id="name"><br> 
   Address: 
   <textarea name="address" id="address"></textarea><br>
   <button type="button" name="reset_btn" id="reset_btn">Reset</button>
</form>
</body>
</html>

In the above code, I have created the HTML form. There are two fields and a reset button inside the HTML form. When you will click on the reset button, values will be clear from both HTML form fields. 

 


Comment

(9.4K Points)
(edited)
2

I was doing this for all fields using the val(' ') method in jQuery but this is not a better way for multiple fields in a form. We use the reset() method in JavaScript but the reset() function is not available in the jQuery library. We use the trigger() method in jQuery to trigger the reset method of JavaScript. Now, you can reset unlimited form fields with form_id with a button click. Let's understand with an example. 

<!DOCTYPE html>
<html>
<head>
   <title>Reset form fields in jQuery with trigger() method</title>
</head>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#reset_btn").click(function(){
        $("#formid").trigger("reset");
     });
});
</script>
<body>
<form action="" method="" id="formid">
   First Name: 
   <input type="text" name="name" id="name"><br>
    Last Name: 
   <input type="text" name="name" id="lname"><br> 
   Address: 
   <textarea name="address" id="address"></textarea><br>
   <button type="button" name="reset_btn" id="reset_btn">Reset</button>
</form>
</body>
</html>

In the above example, we have used the jQuery trigger() method to trigger the reset method of Javascript and as you know well, the reset() method is used to reset form fields. You can add multiple fields and reset on button click in jQuery. 


Comment

Related Questions :-

Your Answer

You can post your answer after login..