X



Home>Tutorial>

What is JavaScript reset() Method

Nuelson Penuel Wednesday, August 2, 2023 Javascript

In JavaScript, the reset() method is a built-in function that is used to reset or clear all the form elements in an HTML form. It clears the user-inputted values or sets them back to their default values. The reset() method is typically associated with an HTML form element and is triggered when a reset button is clicked or the reset() method is called programmatically using JavaScript. Here is an example of how to use the reset() method: HTML:
<form id="myForm">
  <input type="text" name="name" placeholder="Enter your name"><br>
  <input type="email" name="email" placeholder="Enter your email"><br>
  <input type="reset" value="Reset">
</form>
JavaScript:
const form = document.getElementById("myForm");
form.reset();
In the above example, we have an HTML form with two input fields and a reset button. The form has an ID of "myForm", which is used to select the form element in JavaScript. When the reset button is clicked or the reset() method is called programmatically, JavaScript will automatically reset all the form elements within the "myForm" form. This means that it will clear the input values in the text fields and set them back to their default values (if any). It's important to note that the reset() method only works on form elements and not on individual input elements. It affects all form fields within the specified form. Additionally, the reset() method does not clear any styling or formatting applied to the form elements. It only resets the values of the form fields. If you want to reset the styling as well, you would need to manually reset the CSS properties using JavaScript or specify a separate style sheet for resetting styles.

| | Views(281)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services