Javascript Reference - HTML DOM Textarea Object








The Textarea object represents an HTML <textarea> element.

Textarea Object Properties

Property Description
autofocus If a textarea can auto focus when the page loads
cols Sets or gets the cols attribute of a textarea
defaultValue Sets or gets the default value of a textarea
disabled Disable or enable the textarea
form Get the form that contains the textarea
maxLength Sets or gets the maxlength attribute of a textarea
name Sets or gets the name attribute of a textarea
placeholder Sets or gets the placeholder attribute of a textarea
readOnly Sets or gets if a textarea is read-only
required Sets or gets whether the textarea must be filled before submitting a form
rows Sets or gets the rows attribute of a textarea
type Get the type of the textarea is
value Sets or gets the contents of a textarea
wrap Sets or gets the wrap attribute of a textarea




Textarea Object Methods

Method Description
select() Selects the entire contents of a textarea

Standard Properties and Events

The Textarea object supports the standard properties and events.

Example

We can access a <textarea> element by using getElementById().


<!DOCTYPE html>
<html>
<body>
<textarea id="myTextarea">
</textarea>
<button type="button" onclick="myFunction()">test</button>
<p id="demo"></p>
<!-- ww  w.j  a v a2 s . c  om-->
<script>
function myFunction() {
    var x = document.getElementById("myTextarea").value;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

We can create a <textarea> element by using the document.createElement() method.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--from ww  w .j  a v a2 s . com-->
<script>
function myFunction() {
    var x = document.createElement("TEXTAREA");
    var t = document.createTextNode("this is a test");
    x.appendChild(t);
    document.body.appendChild(x);
}
</script>
</body>
</html>

The code above is rendered as follows: