Cover Image for JavaScript reset
123 views

JavaScript reset

The JavaScript reset() method is used to reset the values of form elements to their default values. It is typically called on a form element to clear the input fields and reset the form to its initial state.

Here’s an example that demonstrates how to use the reset() method:

HTML<span role="button" tabindex="0" data-code="<form id="myForm"> <input type="text" name="name" value="John Doe"> <input type="email" name="email" value="[email protected]"> <button type="button" onclick="resetForm()">Reset</button> </form> <script> function resetForm() { var form = document.getElementById("myForm"); form.reset(); }
<form id="myForm">
  <input type="text" name="name" value="John Doe">
  <input type="email" name="email" value="[email protected]">
  <button type="button" onclick="resetForm()">Reset</button>
</form>

<script>
  function resetForm() {
    var form = document.getElementById("myForm");
    form.reset();
  }
</script>

In this example, we have a form with two input fields and a reset button. When the button is clicked, the resetForm() function is called, which retrieves the form element using getElementById() and calls the reset() method on it. This resets the input fields to their initial values.

It’s important to note that the reset() method only resets the values of form elements and does not clear any additional state or data associated with the form.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS