--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; });
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.
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.
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.
$('#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
$('#message').html('');
You can use this method too. Because everything between the open and close tag of textarea is html code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With