Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty the contents of a textarea after submit [duplicate]

I have a comment box (textarea) inside a dialog. If the comment is successfully saved I want to clear the contents of the textarea and close the dialog box. ATM the dialog box will close but I need to wipe its contents.

<textarea id="CommentBox" type="text" runat="server" rows="7" 
maxlength="2000" /> 

if (CommentSuccessfullyUpdated == "TRUE") 
{
//empty the comment box??
//something like
$("#CommentBox").empty();

//closes the dialog box
$("#dialog").dialog('close');

Thanks for any replies


Edit: Thanks for the help guys. It is running through the code but its not working. I think it has to do with in order to pick up the correct vales and resolve a biding issue I had to use:

 function SubmitButton() {
            var commentBoxData = $('#<%=CommentBox.ClientID%>').val();
           }

When run through with a breakpoint returns:

function SubmitButton() {
            var commentBoxData = $('#ctl00_ContentPlaceHolder1_CommentBox').val();
}

AND:

<textarea name="ctl00$ContentPlaceHolder1$CommentBox" id="ctl00_ContentPlaceHolder1_CommentBox" type="text" rows="7" maxlength="2000"> </textarea> 

So Im guessing im not referencing the same textarea when I try to empty it. Also tried

$("#CommentBox.ClientID").val('');

but no joy....ay ideas?

like image 579
Mick Avatar asked Mar 21 '13 10:03

Mick


People also ask

How do I empty the text area after a submit?

To clear an input field after submitting: Add a click event listener to a button. When the button is clicked, set the input field's value to an empty string. Setting the field's value to an empty string resets the input.

What is textarea value?

The value property sets or returns the contents of a text area. Note: The value of a text area is the text between the <textarea> and </textarea> tags.


3 Answers

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

Use the val() method, passing in an empty string.

Documentation: http://api.jquery.com/val

Also, your mark up is wrong. textarea isn't a self-closing element. You need a </textarea> tag. And the type="text" isn't necessary (probably not actually valid either)

As per your edit, you can either set the IDs to be static at the top of your .aspx file (I think it's ClientID="static")

Or you can use a different selector:

$('textarea').filter('[id*=CommentBox]').val('');
like image 101
ahren Avatar answered Oct 15 '22 00:10

ahren


You can use val:

$("#CommentBox").val('');

http://api.jquery.com/val/

JSFiddle

http://jsfiddle.net/KhPM6/1/

Edit:

You are not referencing the ASP.NET generated text area correctly. As you have shown in your question, you need to reference it like:

$('#<%=CommentBox.ClientID%>').val('');
like image 37
Darren Avatar answered Oct 15 '22 01:10

Darren


   $('textarea#CommentBox').val('');
like image 25
Samy Avatar answered Oct 14 '22 23:10

Samy