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 badges4 Answers
Reset to default 4Why 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 textarea
s 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.