javascript - Textarea placeholder text - Stack Overflow

Struggling to understand why my placeholder in my textarea isnt showing yet the input is.<textarea v

Struggling to understand why my placeholder in my textarea isnt showing yet the input is.

<textarea value="Begin your message..." onfocus="ClearPlaceHolder (this)" onblur="SetPlaceHolder (this)" class="contactForm" ></textarea>    

<script type="text/javascript">
    function ClearPlaceHolder (input) {
        if (input.value == input.defaultValue) {
            input.value = "";
        }
    }
    function SetPlaceHolder (input) {
        if (input.value == "") {
            input.value = input.defaultValue;
        }
    }
</script> 

I tied replacing where it says input in this script but it dosnt change anything?

Struggling to understand why my placeholder in my textarea isnt showing yet the input is.

<textarea value="Begin your message..." onfocus="ClearPlaceHolder (this)" onblur="SetPlaceHolder (this)" class="contactForm" ></textarea>    

<script type="text/javascript">
    function ClearPlaceHolder (input) {
        if (input.value == input.defaultValue) {
            input.value = "";
        }
    }
    function SetPlaceHolder (input) {
        if (input.value == "") {
            input.value = input.defaultValue;
        }
    }
</script> 

I tied replacing where it says input in this script but it dosnt change anything?

Share Improve this question asked Jul 24, 2014 at 9:34 stackstack 1034 silver badges10 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

Why not just use the HTML placeholder for this purpose?

<textarea placeholder="Please type something here..."></textarea>

I'd be more elegant in my opinion and it doesn't require any JS.

It is because textarea doesn't have the attribute value.
A textareas value is between the opening and closing tags:

<textarea onfocus="ClearPlaceHolder(this)" 
          onblur="SetPlaceHolder(this)" 
          class="contactForm">Begin your message...</textarea>

Fiddle

You can use the attribute placeholder:

<textarea placeholder="placeholder string"></textarea>

Also remember that textarea doesn't have value attribute as you can read in http://www.w3schools./tags/tag_textarea.asp Value is a property so the statment you are using is correct for putting text in the text area

input.value = "text";

But remember that creating and changing a value attribute doesn't change value property.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742277174a4413808.html

相关推荐

  • javascript - Textarea placeholder text - Stack Overflow

    Struggling to understand why my placeholder in my textarea isnt showing yet the input is.<textarea v

    23小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信