replaceData (W3C DOM Core method)

Spec
Version Depr. Static
DOM1 No No
Browser support (more…)
IE6+ FF1.5+ SA1.3+ OP9+
Full Full Full Full
Returns
void
Throws
INDEX_SIZE_ERR, NO_MODIFICATION_ALLOWED_ERR

Example

var sentence = document.createTextNode
    ("When you're moaning, the whole world smiles with you.");

sentence.replaceData(12, 7, 'smiling');

In the example above we create a Text node with the value When you're moaning, the whole world smiles with you., then we use the replaceData method to replace seven characters starting from character offset 12 (moaning) with smiling.

That operation changes the Text node so it now has the data (and nodeValue) When you're smiling, the whole world smiles with you., and a length of 53.

Then if we perform an additional replacement, starting from character offset 21 of the modified node, until the end of the string (the second argument is longer than the total length of the string, which will replace everything from the initial offset to the end), and replace that with the string the sun comes shining through.:

sentence.replaceData(21, 999, 'the sun comes shining through.');

We'll finally end up with a Text node that has the data (and nodeValue) When you're smiling, the sun comes shining through., and a length of 51.

Arguments

offset (unsigned long) required

The character offset from which to start replacing.

count (unsigned long) required

The number of characters to replace. If offset plus count exceeds the length of the data then everything from offset to the end is replaced (equivalent to calling deleteData with the specified offset and count, followed by appendData with the specified string).

string (DOMString) required

The string with which the specified range is to be replaced.

Description

Insert a string to replace the character data between specified offsets, equivalent to deleteData and insertData in a single operation.

After a successful operation, the data and length properties of the node reflect the change.

Tip: JavaScript has more powerful native features

I've never used this method — JavaScript's built-in string manipulation methods are far more flexible and powerful than anything offered by the DOM.

Compatibility

Internet Explorer Firefox Safari Opera
5.5 6.0 7.0 1.5 2.0 3.0 1.3 2.0 3.0 9.0 9.5
Partial Full Full Full Full Full Full Full Full Full Full

Internet Explorer 5.5 in HTML doesn't implement this method (it returns undefined).

User-contributed notes

There are no comments yet.

Related Products