Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to empty the message in a text area with jquery?

Tags:

jquery

--UPDATED--

I want to empty message in a textarea in callback.

Can anyone tell me how to empty it please?

I tried $("#message").empty(), but it does not empty it.

 <form method="post" id="form" action="index.php/admin/messages/insertShoutBox">             <input name="user" id="nick" value="admin" type="hidden">     <p class="messagelabel"><label class="messagelabel">Message</label>     <textarea id="message" name="message" rows="2" cols="40"></textarea>     <input disabled="disabled" id="send" value="Sending..." type="submit"></p>           </form> 

Full code

 $("#form").submit(function(){     if(checkForm()){         var nick = inputUser.attr("value");         var message = inputMessage.attr("value");         //we deactivate submit button while sending         $("#send").attr({ disabled:true, value:"Sending..." });         $("#send").blur();         //send the post to shoutbox.php         $.ajax({             type: "POST",              url: "index.php/admin/dashboard/insertShoutBox",              data: $('#form').serialize(),             complete: function(data){                 messageList.html(data.responseText);                 updateShoutbox();                  $('#message').val('').empty();                 //reactivate the send button                         $("#send").attr({ disabled:false, value:"SUBMIT !" });             }          });     }     else alert("Please fill all fields!");     //we prevent the refresh of the page after submitting the form     return false; }); 
like image 855
shin Avatar asked Jan 16 '10 16:01

shin


People also ask

How do I make textarea blank?

To clear the value of a textarea element, set it's value property to an empty string, e.g. textarea. value = '' . Setting the element's value to an empty string removes any of the text from the element. Here is the HTML for the examples in this article.

Is empty in JQuery?

The empty() method removes all child nodes and content from the selected elements. Note: This method does not remove the element itself, or its attributes. Tip: To remove the elements without removing data and events, use the detach() method. Tip: To remove the elements and its data and events, use the remove() method.

How do you clear textarea After submit react?

To clear input values after form submit in React: Store the values of the input fields in state variables. Set the onSubmit prop on the form element. When the submit button is clicked, set the state variables to empty strings.


2 Answers

$('#message').val(''); 

Explanation (from @BalusC):
textarea is an input element with a value. You actually want to "empty" the value. So as for every other input element (input, select, textarea) you need to use element.val('');.

Also see docs

like image 112
Matt Avatar answered Sep 29 '22 03:09

Matt


$('#message').html(''); 

You can use this method too. Because everything between the open and close tag of textarea is html code.

like image 26
jarijira Avatar answered Sep 29 '22 03:09

jarijira