The text type represents the plain text of an HTML document's element, which may or may not include escaped HTML characters. However, the HTML code is excluded from the document's text node.

Text node has following characteristics:

  • nodeType: 3
  • nodeName : #text
  • nodeValue : Text contained in the node.
  • parentNode : Element
  • childNode : Not supported.

The text type methods

The following methods allow manipulation of text type:

  • appendData(text) – Appends ‘text’ data to the end of node.
  • deleteData(offset, count) – Deletes ‘count’ number of characters starting at ‘offset’ position.
  • insertData(offset, text) – Inserts ‘text’ data at ‘offset’ position.
  • replaceData(offset, count, text) – Replaces the text starting at index ‘offset’ with ‘text’ data upto ‘count’ letters.
  • splitText(offset) – Splits ‘text’ node at ‘offset’ position into two parts.
  • substringData(offset, count) – Extracts the string from ‘offset’ position upto ‘count’ position.
  • normalize() – This combines two or more text nodes into one text node with nodeValue being concatenation of individual nodeValues.
  • nodeValue.length or data.length – Returns length of the characters in text node. Space is also considered as a text node, and returns length of 1.
  • createTextNode(“text string”) – Creates new text nodes accepting text string to be inserted to node as argument.

Example how to add multiple text elements to a paragraph and normalize them


›› go to examples ››