The Data Object
The CharacterData interface gives you a set of properties and methods to access and manipulate character data. It extends the Node object. No document objects correspond directly with the Data object. The Text object, Comment object, and CDATASection object inherit from it.
Table 16.12 shows the properties of the CharacterData object.
| Table 16.12 CharacterData Object Attributes/Properties | |||
|
|
Name | Value | |
|---|---|---|---|
|
|
data | The character data (string) of the node | |
| length | The number of characters | ||
|
|
|||
Its methods are shown in Table 16.13.
| Table 16.13 CharacterData Object Methods | |
|
|
|
| Name | Returns |
|---|---|
|
|
|
| substringData(offset,count) | The specified substring, where start specifies the start offset, and count specifies the number of characters |
| appendData(arg) | Nothing, but appends the string arg to the end of the data in the object |
| insertData(offset,arg) | Nothing, but inserts the string arg at the specified offset |
| deleteData(offset,count) | Nothing, but removes a range of characters, specified by offset and count |
| replaceData(offset,count,arg) | Nothing, but replaces the characters starting at offset and count along with the string specified in arg |
|
|
|
You'll see an example of using the appendData method in listing 16.12.
Listing 16.12 Getting and Changing Text
1: //var node holds a Node Object
2: if (node.nodeType == 3) //if text
3: var content = node.nodeValue;
4: content.appendData(" !");
| If the variable node is of type text node, then its content is placed inside the content variable. An exclamation point is appended to this content. |