Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I clear a textarea on focus?

Im using a simple form with a textarea, when the users clicks onto the textarea I want the contents of the textarea to be cleared.

Is this possible?

like image 271
CLiown Avatar asked Sep 16 '10 18:09

CLiown


People also ask

How do you clear the text field in focus?

onfocus="this. value='';"/> . This way, the field will be cleared when it gains focus.

How do I clear a textarea?

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.

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.

How do I add focus to textarea?

Answer: Use the jQuery . focus() method You can simply use the jQuery . focus() method to set the focus on the first input box or textarea inside the Bootstrap modal when it is loaded upon activation.


2 Answers

$('textarea#someTextarea').focus(function() {    $(this).val(''); }); 
like image 123
Jacob Relkin Avatar answered Sep 23 '22 12:09

Jacob Relkin


If you only want to delete the default text (if it exists), try this:

$("textarea").focus(function() {      if( $(this).val() == "Default Text" ) {         $(this).val("");     }  }); 

By testing for the default text, you will not clear user entered text if they return to the textarea.

If you want to reinsert the default text after they leave (if they do not input any text), do this:

$("textarea").blur(function() {      if( $(this).val() == "" ) {         $(this).val("Default Text");     }  }); 

Of course, the above examples assume you begin with the following markup:

<textarea>Default Text</textarea> 

If you want to use placeholder text semantically you can use the new HTML5 property:

<textarea placeholder="Default Text"></textarea> 

Although this will only be supported in capable browsers. But it has the added advantage of not submitting the placeholder text on form submission.

like image 31
kingjeffrey Avatar answered Sep 24 '22 12:09

kingjeffrey