You are here: Reference > JavaScript > client-side > HTML DOM > objects > nodes and tags > textarea

textarea object

Browser support:
Creates a multiline text input control.
This control is one of the form controls.
The contents of the control can be submitted to a server if the following conditions are met:
  • A form element must contain the textarea element.
  • The action property of the container form must be set to the URL of the server.
  • The name property of the textarea element must be specified and non-empty.
When the container form is submitted, the name and the text content of the control are sent as a name/value pair.
You can access the text content of the control with the value property. Sometimes it is useful to check the contents of a textarea element before submitting the container form. See Example 2 for details.
If you need a single-line text input control, see the page for the input:text property.

Syntax:

Methods that return the object:
document.createElement ("textarea")
The base interface, through which you can add new functionalities to the textarea object, is the HTMLTextAreaElement interface.
If you want to see the HTML objects by categories, please visit this page.
HTML page for this element: textarea

Possible members:

Properties
Methods
Events
Style properties
accessKey
Sets or retrieves an access key to an element.
all
Represents a collection of all elements contained by an element or the entire document.
attributes
Represents a collection of attribute nodes that belong to an element.
baseURI
10
Returns the base URL for the object.
behaviorUrns
Represents a collection of the Uniform Resource Names for all behaviors attached to an element.
canHaveChildren
Retrieves a Boolean value that indicates whether the element can contain child elements.
canHaveHTML
Retrieves a Boolean value that indicates whether the element can contain HTML formatted text.
childElementCount
93.5
Returns the number of element nodes that are direct descendants of the current element.
childNodes
Represents a collection of all nodes that are direct descendants of an element.
children
3.5
Represents a collection of all element nodes that are direct descendants of an element.
className
Sets or retrieves the style class or classes that belong to the element.
clientHeight
Returns the height of the visible area for an object, in pixels. The value contains the height with the padding, but it does not include the scrollBar, border, and the margin.
clientLeft
Returns the width of the left border in pixels.
clientTop
Returns the height of the top border in pixels.
clientWidth
Returns the width of the visible area for an object, in pixels. The value contains the width with the padding, but does not include the scrollBar, border, and the margin.
cols
Specifies or returns the width of a textarea, in characters.
contentEditable
3
Sets or retrieves whether the contents of the object are editable.
currentStyle
Represents the computed style settings for an element.
dataFld
Specifies or returns which field of a given data source should be bound to the specified object.
dataSrc
Sets or retrieves the identifier of the data source that is bound to the element.
defaultValue
Specifies or returns the initial value of the object. The initial state can be set with the value attribute in HTML.
dir
Sets or retrieves the text direction as related to the lang property.
disabled
Sets or retrieves the state of an object for user interaction.
draggable
3.55
Sets or retrieves whether an element is draggable.
filters
Represents a collection of all filter objects applied to an element.
firstChild
Returns a reference to the first child of the current element.
firstElementChild
93.5
Returns a reference to the first child element of the current element.
form
Returns a reference to the form element in which the object is placed.
forms
Represents a collection of all form elements in the current document.
hideFocus
Specifies or returns whether a dotted rectangle (focus rectangle) is drawn around an object while it has focus.
id
Sets or retrieves a unique identifier for the object.
innerHTML
Sets or retrieves the inner HTML content (the source code between the opening and closing tags) of an element.
innerText
Sets or returns the text content of an element including the text content of its descendants.
isContentEditable
Returns a Boolean value that indicates whether the contents of the object are editable by the user.
isDisabled
Returns a Boolean value that indicates whether the object is disabled.
isMultiLine
Returns a Boolean value that indicates whether the contents of an element can be multiline or not.
isTextEdit
Returns a Boolean value that indicates whether the createTextRange method can be used for the element.
lang
Specifies or returns the language of the element.
language
Sets or retrieves the scripting language for the current element. Use it only for the script element.
lastChild
Returns a reference to the last child of the current element.
lastElementChild
93.5
Returns a reference to the last child element of the current element.
localName
9
Returns the local part of the qualified name of the current node.
name
Sets or retrieves the name of a form control that affects the contents of the message submitted to the server.
namespaceURI
93.6
Sets or returns the namespace URI of the current node.
nextElementSibling
93.5
Returns a reference to the next child element of the current element's parent.
nextSibling
Returns a reference to the next child of the current element's parent.
nodeName
Returns the name of the current node.
nodeType
Returns an integer that indicates the type of the node.
nodeValue
Sets or returns the value of the current node.
offsetHeight
Returns the height of the visible area for an object, in pixels. The value contains the height with the padding, scrollBar, and the border, but does not include the margin.
offsetLeft
Returns the left position of an object relative to the left side of its offsetParent element, in pixels.
offsetParent
Returns a reference to the closest ancestor element in the DOM hierarchy from which the position of the current element is calculated.
offsetTop
Returns the top position of the object relative to the top side of its offsetParent element, in pixels.
offsetWidth
Returns the width of the visible area for an object, in pixels. The value contains the width with the padding, scrollBar, and the border, but does not include the margin.
outerHTML
Sets or retrieves the outer HTML content (the source code including the opening and closing tags) of an element.
outerText
Sets or returns the text content of an element including the text content of its descendants.
ownerDocument
Returns the document object that contains the current node.
parentElement
Returns the parent element of the object in the DOM hierarchy.
parentNode
Returns the parent element of the current node in the DOM hierarchy.
parentTextEdit
Returns the closest ancestor element of the current element in the DOM hierarchy that can be used to create a TextRange object.
previousElementSibling
93.5
Returns a reference to the previous child element of the current element's parent.
previousSibling
Returns a reference to the previous node of the current element's parent.
readOnly
Sets or retrieves whether the contents of the element are changeable.
readyState
Returns a string value that represents the state of the object.
recordNumber
Returns an integer value that indicates the ordinal position of the item within the data set that generated the current object.
rows
Sets or retrieves the number of visible rows within a textarea element.
runtimeStyle
Represents the overridden style settings for an element.
scopeName
Retrieves the local name of the namespace declared for the current element.
scrollHeight
Returns the total height of an element's contents, in pixels. The value contains the height with the padding, but does not include the scrollBar, border, and the margin.
scrollLeft
Sets or retrieves the number of pixels by which the contents of an object are scrolled to the left.
scrollTop
Sets or retrieves the number of pixels by which the contents of an object are scrolled upward.
scrollWidth
Returns the total width of an element's contents, in pixels. The value contains the width with the padding, but does not include the scrollBar, border, and the margin.
selectionEnd
9
Specifies or returns the end position of the selected text within the current element.
selectionStart
9
Specifies or returns the start position of the selected text within the current element.
sourceIndex
Returns the position of the current object in the all collection of the document.
spellcheck
Sets or retrieves whether the automatic spellchecker is enabled.
status
Returns a Boolean value that indicates whether the current element is selected.
style
Represents the inline style settings for an element or a CSS rule.
tabIndex
Specifies or returns the tabbing order for keyboard navigation using the TAB key.
tagName
Returns the tag name of the current element.
tagUrn
Sets or retrieves the Uniform Resource Name (URN) of the namespace declared for the current element.
textContent
9
Sets or returns the text content of an element including the text content of its descendants.
textLength
5
Returns the length of the text in a form control.
title
Specifies or returns a tooltip for an element.
type
Returns the type of a textarea element.
uniqueID
Returns the unique identifier generated by the browser for the object.
unselectable
Sets or retrieves whether the selection process can start in an element's content.
value
Specifies or returns the value of the control.
wrap
Specifies or returns the word wrapping behavior.

Example HTML code 1:

This example illustrates the use of the textarea element:
<form method="post" action="#URL#">
    Message:<br />
    <textarea name="message" cols="40" rows="5">Test message</textarea>
    <br /><br />
    <input type="submit" value="Send" />
</form>
Did you find this example helpful? yes no

Example HTML code 2:

This example checks the contents of the textarea element before submitting the form:
<head>
    <script type="text/javascript">
        function CheckAndSubmit () {
            var regForm = document.getElementById ("regForm");
            var message = document.getElementById ("message");

            if (message.value.length == 0) {
                alert ("Empty message cannot be sent!");
                return;
            }

            regForm.submit ();
        }
    </script>
</head>
<body>
    <form id="regForm" method="post" action="#URL#">
        Message:<br />
        <textarea name="message" id="message" cols="40" rows="5"></textarea>
        <br /><br />
        <input type="button" value="Send" onclick="CheckAndSubmit ()" />
    </form>
</body>
Did you find this example helpful? yes no

Example HTML code 3:

This example shows how to get the selected text in a textarea element:
<head>
    <script type="text/javascript">
        function GetSelection () {
            var selection = "";

            var textarea = document.getElementById("myArea");
            if ('selectionStart' in textarea) {
                    // check whether some text is selected in the textarea
                if (textarea.selectionStart != textarea.selectionEnd) {
                    selection = textarea.value.substring  (textarea.selectionStart, textarea.selectionEnd);
                }
            }
            else {  // Internet Explorer before version 9
                    // create a range from the current selection
                var textRange = document.selection.createRange ();
                    // check whether the selection is within the textarea
                var rangeParent = textRange.parentElement ();
                if (rangeParent === textarea) {
                    selection = textRange.text;

                }
            }

            if (selection == "") {
                alert ("No text is selected.");
            }
            else {
                alert ("The current selection is: " + selection);
            }
        }
    </script>
</head>
<body>
    <textarea id="myArea" spellcheck="false">Select some text within this field.</textarea>
    <button onclick="GetSelection ()">Get the current selection</button>
</body>
Did you find this example helpful? yes no

Example HTML code 4:

This example shows how to modify the selected text in a textarea element:
<head>
    <script type="text/javascript">
        function ModifySelection () {
            var textarea = document.getElementById("myArea");
            if ('selectionStart' in textarea) {
                    // check whether some text is selected in the textarea
                if (textarea.selectionStart != textarea.selectionEnd) {
                    var newText = textarea.value.substring (0, textarea.selectionStart) + 
                        "[start]" + textarea.value.substring  (textarea.selectionStart, textarea.selectionEnd) + "[end]" +
                        textarea.value.substring (textarea.selectionEnd);
                    textarea.value = newText;
                }
            }
            else {  // Internet Explorer before version 9
                    // create a range from the current selection
                var textRange = document.selection.createRange ();
                    // check whether the selection is within the textarea
                var rangeParent = textRange.parentElement ();
                if (rangeParent === textarea) {
                    textRange.text = "[start]" + textRange.text + "[end]";
                }
            }
        }
    </script>
</head>
<body>
    <textarea id="myArea" cols="30" spellcheck="false">Select some text within this field.</textarea>
    <button onclick="ModifySelection ()">Modify the current selection</button>
</body>
Did you find this example helpful? yes no

Example HTML code 5:

This example shows how to create a textarea that does not accept numeric input:
<head>
    <script type="text/javascript">
        function FilterInput (event) {
            var chCode = ('charCode' in event) ? event.charCode : event.keyCode;

                // returns false if a numeric character has been entered
            return (chCode < 48 /* '0' */ || chCode > 57 /* '9' */);
        }
    </script>
</head>
<body>
    The following text field does not accept numeric input:
    <textarea onkeypress="return FilterInput (event)"></textarea>
</body>
Did you find this example helpful? yes no

Related pages:

External links:

User Contributed Comments

Post Content

Post Content