asp.net.ph

Skip Navigation Links

innerHTML Property

DHTML Object Properties   DHTML Objects


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

Syntax


HTML N/A
Script object.innerHTML [ = sHTML ]

Possible Values


sHTML String containing the contents between the start and end tags.

The property is read/write with no default value.

Remarks

The property takes a string containing a valid combination of text and HTML tags, except for HTML, HEAD, and TITLE elements.

When setting the property, the given string completely replaces the existing content of the object. If the string contains HTML tags, the string is parsed and formatted as it is placed into the document.

The property can only be set after the document has loaded. If an element is dynamically created using TextRange, innerHTML, or outerHTML, JScript® [ compatible with ECMA-262 language specification ] can be used to handle events for the newly formed tags. VBScript is not supported.

When inserting script using innerHTML, the SCRIPT tag must include the defer attribute.

InnerHTML is read-only on the following objects: HTML, TABLE, TBODY, TFOOT, THEAD, TR.

Example

The following example shows use of the innerHTML property. The affected text, and any tags within it, are dynamically changed in response to mouse events.

<p id="msg"
   onclick="this.innerHTML='<i>Double click to reset.</i>'"
   ondblclick="this.innerHTML='Click me to change again.'">
      Click me to change my <b>innerHTML</b>.
</p>

This feature requires Microsoft® Internet Explorer® 4.0 or later.

 Show me 

The following example shows use of the innerHTML property to check for the existence of content, and the insertAdjacentHTML method to insert a script into the page if it does not yet exist.

function insertScript ( ) {
   if ( scriptDiv.innerHTML == "" ) {
      var sHTML = "<input type=button onclick='sayHello ( ) ' value='Click Me'>";
      var sScript = "<script defer>";
      sScript += "function sayHello ( ) {";
      sScript += "alert ( 'Hello. This message is from the inserted script.' ) }";
      sScript += "</script>";
      scriptDiv.insertAdjacentHTML ( "afterBegin", sHTML + sScript );
   }
   else alert ( 'The script is already available.' );
}

 Show me 

See Also

innerText, insertAdjacentText, outerText, outerHTML



>

© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note