最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Textarea placeholder text - Stack Overflow

programmeradmin4浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论