-1

Here is my code what i was trying: Markup:

<textarea class="form-control textareaInput" rows="12" id="textareaInput"></textarea>
                    

<div class="row">
     <div class="col-lg-2 text-center">
        <div class="form-group">
             <button  class="form-control btn btn-primary reset" onclick="myFunction()">Reset</button>
         </div>
     </div>
</div>

My function:

function myFunction() {
  //alert("ok");
  document.getElementById("textareaInput").reset();
}

I am getting errors when I click on the reset button.
Error:

Uncaught TypeError: document.getElementById(...).reset is not a function

Where is the problem? N.B: I was not using the form tag.

4
  • 1
    If you know you were not using a form, you know the error, right? Commented May 29, 2016 at 14:35
  • without using form i want to reset textarea.How can i do that? where is my problem? Commented May 29, 2016 at 14:37
  • 3
    You'll have to find an alternative, because reset()requires a HTMLFormElement: developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/reset Commented May 29, 2016 at 14:39
  • The duplicate is wrong. It is regarding clearing input/textarea value, not reseting it. Now i'm quite sure in dupe or in this question or more obviously in both, there is a confusion between cleareing and resetting a input value. Clearing means set to empty value, where resetting means set it to any default value which could be any value, not necessary the empty string. reset() DOM node method of form element set values of any descendant ':input' to default one, not to empty string. Commented May 29, 2016 at 15:02

5 Answers 5

12

You should use

document.getElementById("textareaInput").value = "";
Sign up to request clarification or add additional context in comments.

Comments

3

Without using a form, to reset a textarea or any other input element, just set its value to default one, using DOM node property defaultValue:

function myFunction() {
  document.getElementById("textareaInput").value = document.getElementById("textareaInput").defaultValue;
}

In your case, because default value is empty string, you could just re-set it to empty string but this would be harder to maintain.

Comments

2

The HTMLFormElement.reset() method restores a form element's default values. You Should USE <form> for sure.

Comments

1

The .reset method available only for Form elements (https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/reset), not for textarea or input DOM-elements. So, if you want to clear text in textarea, you can do it with two ways:

  1. document.getElementById("textareaInput").value = '';
  2. Wrap your textarea with form element, and use reset on it.

1 Comment

Just as a side note, the default value isn't always empty string
1

put elements in form tag :

<body>
    <form>
<textarea class="form-control textareaInput" rows="12" id="textareaInput"></textarea>
<div class="row">
     <div class="col-lg-2 text-center">
        <div class="form-group">
             <button  class="form-control btn btn-primary reset" onclick="myFunction()">Reset</button>
         </div>
     </div>
</div>
        </form>
<script>
    function myFunction() {
  //alert("ok");
  document.getElementById("textareaInput").reset();
}
    </script>
</body>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.