reset input field in javascript

To reset an input field in Javascript, you can use the reset() method on the form element that the input field belongs to. Here's an example:

<form>
  <label for="inputField">Input Field:</label>
  <input type="text" id="inputField" name="inputField"><br><br>
  <button type="button" onclick="resetField()">Reset</button>
</form>

<script>
  function resetField() {
    document.getElementById("inputField").form.reset();
  }
</script>
294 chars
12 lines

In this example, the resetField() function is called when the "Reset" button is clicked. When this happens, the reset() method is called on the form element that the input field belongs to, which resets all form elements within the form, including the input field.

gistlibby LogSnag