Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript HTML DOM Change HTML Content

JavaScript HTML DOM - Changing HTML


The HTML DOM allows JavaScript to change the content of HTML elements.


Changing the HTML Output Stream

JavaScript can create dynamic HTML content:

**Today's date is: **

In JavaScript, document.write() can be used to write content directly to the HTML output stream.

Example

Try it »

lamp Never use document.write() after the document (DOM) has finished loading. It will overwrite the document.

Change HTML Content

The simplest way to modify HTML content is to use the innerHTML property.

To change the content of an HTML element, use this syntax:

document.getElementById(

id

).innerHTML=

New HTML

This example changes

element's content changes:

Example

Hello World!

Try it »

This example changes

element's content changes:

Example

Try it »

Example explained:


Change HTML Attributes

To change the attribute of an HTML element, use this syntax:

document.getElementById(

id

).

attribute=new attribute value

This example changes The src attribute of the element:

Example

Try it »

Example explained: