Browser/User Agent Support
| IE | Mozilla | Netscape | Opera | Safari | Netscapte | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
|---|
Constructors
| Constructor | IE | Mozilla | Netscape | Opera | Safari | Netscapte |
|---|---|---|---|---|---|---|
Represents a text node in a document. | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
Methods
| Method | IE | Mozilla | Netscape | Opera | Safari | Netscapte |
|---|---|---|---|---|---|---|
Splits this node into two adjacent nodes at the given offset | 6.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ | no |
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 Text nodes will be merged into one.
References
CharacterData | Node.normalize
Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
Constructor Detail
Method Detail
splitText(Number offset) : static Text
Splits this node into two adjacent nodes at the given offset
| Number | offset | Number of characters from the beginning of the text string to split the node. |
-
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:
- Remarks
-
This method is used to split a
Textnode into two halves, split on a specific character offset. The second half of the text is returned as a newTextnode, 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
CharacterDataobject, 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
- Availability
HTML DOM Level 1 | HTML DOM Level 2 | W3C
