1

I'm new to JavaScript, so I'm not sure where to look to do this.

I have a web page where the user can enter a message in a textarea box. I want to have instructions for them in the box, this is just an example:

<TEXTAREA NAME="message" ROWS=10 COLS=80>Enter your text here</TEXTAREA>

When the user clicks within this textarea boxes or starting to enter text, I want it to immediately clear out the instructions text.

I've seen this done on web pages, but I can't find any to examine.

Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
Edward Coast
  • 374
  • 4
  • 17
  • Use the `placeholder` attribute of the ` – 4castle Sep 17 '17 at 18:45
  • 1
    possible duplicate https://stackoverflow.com/questions/1626107/text-in-html-field-to-disappear-when-clicked/5077265#5077265 – Nachshon Schwartz Sep 17 '17 at 18:49
  • 1
    Possible duplicate of [Text in HTML Field to disappear when clicked?](https://stackoverflow.com/questions/1626107/text-in-html-field-to-disappear-when-clicked) – 4castle Sep 17 '17 at 18:50

4 Answers4

2

You should just replace it with a placeholder.

<textarea name="message" rows=10 cols=80 placeholder="Enter your text here"></textarea>

or you could do this by binding event handlers for blur and focus.

let message="Enter your text here";
$('textarea').focus(function(){
  if($(this).val() === message)
       $(this).val('');
});
$('textarea').blur(function(){
  if($(this).val().length === 0)
       $(this).val(message);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea name="message" rows=10 cols=80>Enter your text here</textarea>
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
1

use placeholder to do this

<TEXTAREA NAME="message" ROWS=10 COLS=80 placeholder="Enter your text here"></TEXTAREA>
Nachshon Schwartz
  • 15,289
  • 20
  • 59
  • 98
0

Hello If you want to use javascript or jquery then try the below code

     $(function() {
    var defaultText = 'Enter your text here';
    $('#txt').focus(function() {
        defaultText = $(this).val();
        $(this).val('');
    }).blur(function() {
    if($(this).val().length === 0)
        $(this).val(defaultText); 
        
     });
 })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<TEXTAREA NAME="message" id="txt" ROWS=10 COLS=80 >Enter your text here</TEXTAREA>
Asifuzzaman Redoy
  • 1,773
  • 1
  • 15
  • 30
0

Attach an event on focus to your text area.

//assigns value to yext area

    document.getElementById("textarea").value = "this is an example";

    //does focus event
        document.getElementById("textarea").addEventListener("focus", myFocusMethod, true) ;

        function myFocusMethod(){
        document.getElementById("textarea").value = "";
        }

    //does click
    function doClick() {
    }

<TEXTAREA id="textarea" NAME="message" ROWS=10 COLS=80 onclick="doClick();"></TEXTAREA>
Barr J
  • 10,636
  • 1
  • 28
  • 46
  • Where in the flow of a web page does this get put in the JavaScript? I have a clickon button which calls a function. Would this be part of that function? – Edward Coast Sep 17 '17 at 19:03
  • before the function. this even listenes to the client actions and acts when a client focuses on the text area. updated snippet with comments for you. – Barr J Sep 17 '17 at 19:07