The Text interface represents a text content of an element or attribute. The nodeType value of Text is 3. The Text does not have any children.
The properties of text node are listed below:
data
The data property sets or returns the text of element or attribute.
isElementContentWhitespace
The isElementContentWhitespace property is a Boolean and it returns true if the text node contains whitespace.
length
The length property returns the length of the element or attribute.
wholeText
The wholeText property returns text of text nodes adjacent to this node concatenated in the document order.
appendData()
The appendData() method appends data to the node.
Syntax for appendData() method
textnode.appendData(string);
where,
- string is the string to be added to the textnode.
deleteData()
The deleteData() method deletes data from the node.
Syntax for deleteData() method
textnode.deleteData(start, length);
where,
- start is the index from which the characters has to be removed.
- length specifies the number of characters to be removed.
insertData()
The insertData() method inserts the data to the node.
Syntax for insertData() method
textnode.insertData(start, string);
where,
- start is the index from which the characters has to be inserted.
- string specifies the string to be inserted.
replaceData()
The replaceData() method replaces data in text node.
Syntax for replaceData() method
textnode.replaceData(start,length,string);
where,
- start is the index from which the characters has to be inserted.
- string specifies the string to be replaced.
length specifies how many characters to replace.
replaceWholeText()
The replaceWholeText() method replaces text of this and adjacent nodes with all the specified text.
Syntax for replaceWholeText() method
textnode.replaceWholeText(string);
where,
- string is the text to be replaced.
splitText()
The splitText() method splits this node into two nodes at specified offset. It returns the text which is split after the offset.
Syntax for splitText() method
string = textnode.splitText(offset);
where,
- offset is the text to be replaced.
substringData()
The substringData() method gets data from text node.
Syntax for substringData() method
substringData(start, length);
where,
- start defines the start point to extract characters.
- length specifies how many characters to extract.
Example how to delete a text element and add a new one to a XML
myData.xml
<?xml version='1.0'?>
<root>
<data>
<text>
<author>P C Tejaswi</author>
<book>Karvaalo</book>
<award>State Academy</award>
</text>
<text>
<author>Kuvempu</author>
<book>Ramayana Darshanam</book>
<award>Gnanapeeta</award>
</text>
</data>
</root>
myData.html
<!DOCTYPE html>
<html>
<body>
<h1>DOM usage </h1>
<p id="para">
</p>
<script>
function loadFile(name){
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",name,false);
xhttp.send();
return xhttp.responseXML;
}
xmldoc = loadFile("myData.xml");
x = xmldoc.getElementsByTagName("book")[0].childNodes[0];
x.deleteData(0,8)
x.insertData(0,"Parisara Kathegalu");
document.write(x.nodeValue);
</script>
</body>
</html>
Output of above example:
<book>Karvaalo</book> is replaced to
<book>Parisara Kathegalu</book>
Comments
No comments have been made yet.
Please login to leave a comment. Login now