I know how to remove text in a simple html textbox but html textareas seem much more complicated. instead of the value attribute you put the text right between:
<开发者_JS百科;html>
<textarea> </textarea>.
</html>
This is why im having trouble making an onFocus and onBlur event.
<textarea name="message" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;">
Put anything for default value here
</textarea>
Live example: http://jsfiddle.net/SRYLg/
A textarea behaves like other <input>
elements (with type
text
or password
), instead of having a value attribute, the value is between the <textarea>
and </textarea>
tags.
Accessing and modifying the contents of the textfield is no difference. The below code displays a textarea and an input box. The same function is used for accessing the values and modifying it. If the value equals to "example text" when entering the input, the text is cleared. If the textarea / input box is empty when leaving it, "example text" will be put in it.
<textarea id="field1">example text</textarea>
<input id="field2" value="example text">
<script>
function addEvents(id) {
var field = document.getElementById(id);
field.onfocus = function () {
if (this.value == "example text") {
this.value = "";
}
};
field.onblur = function () {
if (this.value == "") {
this.value = "example text";
}
};
}
addEvents("field1");
addEvents("field2");
</script>
- Live example
Your Javascript should have:
function RemoveText(obj)
{ obj.value = ''; }
And Your HTML element should have:
onfocus="RemoveText(this);"
what about calling a javascript function during the onFocus event?
function emptyText(){
document.getElementById(textarea).innerHTML = "";
}
精彩评论