user2232681
user2232681

Reputation: 833

How to clear text from textarea input?

I have the following form and I would like to clear any user inputs when I close the form. I do so with the following JQ function, however the textarea elements do not clear in the same manner (#reply will only clear using .val("")). I do not understand why that is.

HTML
<div class="form" id="form">
  <form id="postandreply" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" >
    <div id="textarea">
      <label for="posttopic">Topic</label>
  <textarea id="posttopic" name="posttopic" maxlength="100" cols="50" rows="3"></textarea>
      <label for="comment">Comment</label>
      <textarea id="comment" name="posttext" maxlength="500" cols="80" rows="6"></textarea>
      <label for="reply">Reply</label>
      <textarea id="reply" name="replytext" maxlength="500" cols="80" rows="6"></textarea>
   </div>

      <input type="text" id="lookup" name="reply-lookup" />
     <input type="submit" id="submit" name="post" value="Post" />

  </form>
</div>

JQ
// -------------------------reset the forms -------------------------   
function reset(formhtml) {

$('#posttopic, #comment, #reply').text(""); //will not clear #reply
$('#lookup, #reply').val("");              //this will clear #reply

}

Upvotes: 0

Views: 1145

Answers (3)

Lucky Soni
Lucky Soni

Reputation: 6878

How about:

$('#form')[0].reset(); // use the id of the form

Upvotes: 1

MillaresRoo
MillaresRoo

Reputation: 3848

From JQuery documentation:

The .text() method cannot be used on form inputs or scripts. To set or get the text value of input or textarea elements, use the .val() method. To get the value of a script element, use the .html() method.

Upvotes: 2

SamV
SamV

Reputation: 7586

.val() is the main method to get inputted data. Heres a quote from http://api.jquery.com/val/.

The .val() method is primarily used to get the values of form elements such as input, select and textarea. In the case of elements, the .val() method returns an array containing each selected option; if no option is selected, it returns null.

.text() does not work as textarea is still an input.

Upvotes: 2

Related Questions