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

TextNode object

Browser support:
Represents a text as a node.
TextNode objects contain only text content without any HTML or XML markup. TextNode objects make it possible to insert texts into the document as nodes (appendChild, insertBefore).

Syntax:

Methods that return the object:
object.createTextNode (data)
Related objects:
The base interface, through which you can add new functionalities to the TextNode object, is the Text interface.

Possible members:

Properties:
baseURI
10
Returns the base URL for the object.
data
Sets or returns the text content of a CommentNode, TextNode or comment element.
isElementContentWhitespace
Returns a Boolean value that indicates whether the current TextNode only contains whitespaces.
length
Returns the number of characters within a TextNode, CommentNode or comment object.
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.
nodeTypeString
Returns the type of the current node as a string.
nodeValue
Sets or returns the value of the current node.
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.
previousSibling
Returns a reference to the previous node of the current element's parent.
textContent
9
Sets or returns the text content of an element including the text content of its descendants.
Methods:
addEventListener
9
Registers an event handler function (event listener) for the specified event on the current object.
appendData
Appends the specified text content to the end of the current TextNode, CommentNode or comment element.
attachEvent
Registers an event handler function (event listener) for the specified event on the current object.
cloneNode
Returns an exact copy of the current node.
compareDocumentPosition
9
Compares the placement of the specified node with the current node in the DOM hierarchy.
deleteData
Removes the specified part of the text content of the current TextNode, CommentNode or comment element.
detachEvent
Removes the specified event handler from the current element that was registered earlier with the attachEvent method.
dispatchEvent
9
Dispatches the specified event to the current element.
hasAttributes
9
Returns whether the current element has any attributes specified or not.
hasChildNodes
Returns whether the current node has any child nodes or not.
insertData
Inserts text content into the current TextNode, CommentNode or comment element at the specified position.
isDefaultNamespace
9
Returns whether the specified namespace URI is the default namespace in the scope of the current node.
isEqualNode
9
Returns whether the current node is equal to the specified one.
isSameNode
9
Returns whether the current node is the same node as the specified one.
isSupported
9
Returns whether the specified DOM module and version is supported by the current node.
lookupNamespaceURI
9
Retrieves the namespace URI associated with the specified namespace prefix in the scope of the current node.
lookupPrefix
9
Retrieves the namespace prefix associated with the specified namespace URI in the scope of the current node.
normalize
9
Puts the subtree that belongs to the current node into a 'normalized' form.
removeEventListener
9
Removes the specified event handler from the current element that was registered earlier with the addEventListener method.
removeNode
Removes the current element with or without its children from the document tree.
replaceData
Replaces the specified part of the text content of the current TextNode, CommentNode or comment element with a new text.
replaceNode
Replaces the current element with the specified element.
splitText
Breaks the current TextNode object into two TextNode objects at the specified index.
substringData
Returns the part of the current element's text content from the specified position with the specified length.
swapNode
Interchanges the positions of the current element and the specified element in the document hierarchy.

Example HTML code 1:

This example shows how to get a TextNode object:
<head>
    <script type="text/javascript">
        function GetTextNode () {
            var textContainer = document.getElementById ("textContainer");
            var textNode = textContainer.firstChild;
            alert (textNode.data);
        }
    </script> 
</head>
<body>
    <div id="textContainer">This is a simple text in the container.</div>
    <button onclick="GetTextNode ()">Get the contents of the container</button>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example shows how to create and insert a TextNode object:
<head>
    <script type="text/javascript">
        function CreateText () {
            var textContainer = document.getElementById ("textContainer");
            var textNode = document.createTextNode ("Dynamically generated text");
            textContainer.appendChild (textNode);
        }
    </script>
</head>
<body>
    <div id="textContainer"></div>
    <button onclick="CreateText ();">Create a text node!</button>
</body>
Did you find this example helpful? yes no

Related pages:

External links:

User Contributed Comments

Post Content

Post Content