8 out of 17 rated this helpful Rate this topic

innerText Property

.NET Framework 3.0

Sets or retrieves the text between the start and end tags of the object.

Syntax

HTML N/A
Scripting [ sTxt = ] object.innerText

Possible Values

sTxt String that specifies or receives the text between the start and end tags.

The property is read/write. The property has no default value.

DHTML expressions can be used in place of the preceding value(s). As of Internet Explorer 8, expressions are supported in IE7 Standards mode and IE5 (Quirks) mode only. For more information, see About Dynamic Properties and Defining Document Compatibility.

Remarks

The innerText property is valid for block elements only. By definition, elements that do not have both an opening and closing tag cannot have an innerText property.

The innerText property is read-only on the html, table, tBody, tFoot, tHead, and tr objects.

When the innerText property is set, the given string completely replaces the existing content of the object.

You can set this property only after the onload event fires on the window. When dynamically creating a tag using TextRange, innerHTML, or outerHTML, use Microsoft JScript to create new events to handle the newly formed tags. Microsoft Visual Basic Scripting Edition (VBScript) is not supported.

You can change the value of the title element using the document.title property.

To change the contents of the table, tFoot, tHead, and tr elements, use the table object model. For example, use the rowIndex property or the rows collection to retrieve a reference to a specific table row. You can add or delete rows using the insertRow and deleteRow methods. To retrieve a reference to a specific cell, use the cellIndex property or the cells collection. You can add or delete rows using the insertCell and deleteCell methods. To change the content of a particular cell, use the innerHTML property.

security note Security Alert    Use the innerText to safely add dynamic text to a Web page. Unlike other properties, innerText does not execute script when inserting text into the Document Object Model (DOM) of a Web page.

Example

This example uses the innerText property to replace an object's contents. The object surrounding the text is not replaced.


<P ID=oPara>Here's the text that will change.</P>
:
<BUTTON onclick="oPara.innerText='WOW! It changed!'">Change text</BUTTON>
<BUTTON onclick="oPara.innerText='And back again'">Reset</BUTTON>

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/innerText.htm

Standards Information

There is no public standard that applies to this property.

Applies To

A, ABBR, ACRONYM, ADDRESS, APPLET, AREA, ARTICLE, ASIDE, B, BDO, BIG, BLOCKQUOTE, BODY, BR, BUTTON, CAPTION, CENTER, CITE, CODE, COL, COLGROUP, CUSTOM, DD, DEL, DFN, DIR, DIV, DL, DT, EM, EMBED, FIELDSET, FIGCAPTION, FIGURE, FONT, FOOTER, FORM, FRAME, FRAMESET, HEAD, HEADER, HGROUP, hn, HR, HTML, I, IFRAME, IMG, INPUT type=button, INPUT type=checkbox, INPUT type=file, INPUT type=hidden, INPUT type=image, INPUT type=password, INPUT type=radio, INPUT type=reset, INPUT type=submit, INPUT type=text, INS, ISINDEX, KBD, LABEL, LEGEND, LI, LISTING, MAP, MARK, MARQUEE, MENU, NAV, nextID, NOBR, OBJECT, OL, OPTGROUP, OPTION, P, PLAINTEXT, PRE, Q, RT, RUBY, S, SAMP, SECTION, SELECT, SMALL, SPAN, STRIKE, STRONG, SUB, SUP, TABLE, TBODY, TD, TEXTAREA, TFOOT, TH, THEAD, TR, TT, U, UL, VAR, XMP, Element Constructor

See Also

insertAdjacentText
Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ
innerText applies to inline elements as well
This document mentions "The innerText property is valid for block elements only." This doesn't apply to the standard notion of block-level elements as detailed in the HTML4/CSS specification, since inline elements like <a> and <span> are supported. This might be obvious because the documentation here says innerText applies to those elements as well as several others, but the point about block elements was misleading.
avoid document.all
This example uses IE4's "document.all" syntax, which should be considered deprecated.

A more standards compliant example would be:

<P ID="oPara">Here's the text that will change.</P>
:
<BUTTON onclick="document.getElementById('oPara').innerText='WOW! It changed!'">Change text</BUTTON>
<BUTTON onclick="document.getElementById('oPara')
.innerText='And back again'">Reset</BUTTON>

unsymmetric - edit v. replace() - linebreak
Range.text=(rtf/doc clipBoard) creates <BR> for linebreak, and <P> for double linebreaks, in innerText ... Likewise contentEditable Enter creates <P> and Shift-Enter creates <BR>.

However, innerText.replace(RegExp/gmi) doesn't maintain the <P> v. <BR> distinction.

So ... pre-replace <\/?P> with <BR> to get <BR><BR> as a simulated maintenance.