Working with Text

The text content of an element is represented by a Text object, which is presented as a child of the element in the document model.

NameDescriptionReturns
appendData(<string>) Appends the string to the end of text.void
dataGets or sets the text.string
deleteData(<offset>, <count>) Removes the text from the string.void
insertData(<offset>, <string>) Inserts the specified string at the specified offset.void
lengthReturns the number of characters.number
replaceData(<offset>,<count>, <string>) Replaces a region of text with the specified string.void
replaceWholeText(<string>) Replaces all of the text.Text
splitText(<number>) Splits the existing Text element into two at the specified offset.Text
substringData(<offset>, <count>) Returns a substring from the text.string
wholeTextGets the text.string
 
<!DOCTYPE HTML> 
<html> 
    <head> 
        <title>Example</title> 
    </head> 
    <body> 
        <p id="textblock"> 
            This is a test.
        </p> 
        <button id="pressme">Press Me</button> 
        <script> 
            
            var elem = document.getElementById("textblock"); 
            document.getElementById("pressme").onclick = function() { 
                var textElem = elem.firstChild; 
                document.writeln("The element has " + textElem.length + " chars"); 
                textElem.replaceWholeText("This is a new string "); 
            }; 
        </script> 
    </body> 
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    DOM  

DOM Model:
  1. Document Object Model
  2. nodeName and nodeValue Properties
  3. Node Relationships:childNodes
  4. parentNode
  5. previousSibling and nextSibling properties
  6. lastChild and firstChild
  7. appendChild() adds a node to the end of the childNodes list
  8. insertBefore()
  9. ownerDocument property
  10. removeChild
  11. replaceChild()
  12. Working with Text
  13. Check the length of NodeList
  14. Convert NodeList to an Array
  15. html tag and its cooresponding JavaScript class