Text : CharacterData
Return to: HTML DOM Level 2 index

Represents a text node in a document.

Platform Support

IE Mozilla Netscape Opera Safari
5.0+ 1.0+ 6.0+ 7.0+ 1.0+

Constructors

Constructor Action IE Mozilla Netscape Opera Safari
Represents a text node in a document.
Show Details 5.0+ 1.0+ 6.0+ 7.0+ 1.0+

Text() : Text

Represents a text node in a document.

Returns
Text

Visibility
internal

Functions

Method Action IE Mozilla Netscape Opera Safari
static splitText(Number offset) : Text
Splits this node into two adjacent nodes at the given offset
Show Details 6.0+ 1.0+ 6.0+ 7.0+ 1.0+

Parameters
Number offset Number of characters from the beginning of the text string to split the node.

Returns
Text

Using splitText

/* Given the source XML, bold the word "everything":
                        

Life, the universe, and everything

*/ var element = document.Document.getElementById('title'); var textNode1 = element.Node.childNodes[0]; if (textNode1.Node.nodeType == Node.Node.TEXT_NODE) { var offset = textNode1.CharacterData.data.String.indexOf('everything'); var textNode2 = textNode1.splitText(offset); var boldElement = element.Node.appendChild(document.Document.createElement('b')); boldElement.Node.appendChild(textNode2); }

For more examples, see the quirksmode test page:

http://www.quirksmode.org/dom/tests/splittext.html

Remarks

This method is used to split a Text node into two halves, split on a specific character offset. The second half of the text is returned as a new Text node, and is automatically inserted adjacent to the first one in the DOM tree. This method results in no visual change, except the text nodes can be modified to change the structure of the text (e.g. wrapping a block of text in another element, like an HTML "bold" tag).

Manipulating text content is possible by using some of the methods on the CharacterData object, but this is usually more work than developers want to go through. This method provides a convenience method for a common pattern in manipulating DOM text.

Throws
Raises an INDEX_SIZE_ERR error if the offset supplied to this method is negative or is greater than the number of characters in this node.
Raises a NO_MODIFICATION_ALLOWED_ERR error if this node is read-only.
See Also

Node.normalize

Availability

HTML DOM Level 1|HTML DOM Level 2|W3C

Remarks

This object interface represents the text contents, or character data, of an Element or Attr object.

If an Element only has text and no other elements as its children, then there will be a single Text node representing all this content. Assuming there are other elements as it's children however, there will be multiple Text nodes separating the Element objects from each other. It is also possible for multiple Text nodes to be adjacent to one another, though when the document is saved (or when the parent Node's Node.normalize method is invoked) these multiple Text nodes will be merged into one.

References

CharacterData|Node.normalize

Availability

HTML DOM Level 1|HTML DOM Level 2|W3C

text_javascript aptana_docs