14 out of 94 rated this helpful - Rate this topic

TextRange object

[This documentation is preliminary and is subject to change.]

Represents text in an HTML element.

Standards information

There are no standards that apply here.

Remarks

Use this object to retrieve and modify text in an element, to locate specific strings in the text, and to carry out commands that affect the appearance of the text.

To retrieve a text range object, apply the createTextRange method to a body, button, or textArea element or an input element that has TYPE text.

Modify the extent of the text range by moving its start and end positions with methods such as move, moveToElementText, and findText. Within the text range, you can retrieve and modify plain text or HTML text. These forms of text are identical except that HTML text includes HTML tags, and plain text does not.

Examples

This example changes the text of a button element to "Clicked" through the TextRange object.


<SCRIPT LANGUAGE="JScript">
var b = document.all.tags("BUTTON");
if (b!=null) {
    var r = b[0].createTextRange();
    if (r != null) {
        r.text = "Clicked";
    }
}
</SCRIPT>

See also

createTextRange

 

 

Build date: 2/14/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Bringing some HTML5 W3C Range object properties to IE8-
The code is huge so I pasted it at https://gist.github.com/1115251 .
It is just the IE8- equivalent of HTML5 W3C startContainer,endContainer,startOffset and endOffset.
Caution - Anomalies

1. .text property is unstable (character copy-in/find)

e.g. textrange.text = "·" // &middot;

Sometimes copies-in as "·" (probably &middot; rendered)
Sometimes copies-in as "•" (probably &boule; rendered)

(This was noticed in copying text from rtf to contenteditable..)

2. object definition is unstable (onclick, on-arrow)

When text-selection is attempted by mouse-click on an object of position:absolute or width:defined, this object is a collection of one html element.
Similarly, When text-selection is attempted by arrow-key, it won't-select: The attempt moves the scrollbar instead.

REMEDY (script):
a. detect .length==1 (otherwise undefined);
b. detect .text==undefined (otherwise defined and maybe blank).

REMEDY (user): doubleclick.

(This was noticed in contenteditable.)