Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST

Basic JavaScript

JS Tutorial JS Syntax JS Variables JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Scope JS Dates JS Arrays JS Sets JS Maps JS Iterations JS Math JS RegExp JS Data Types JS Errors JS Conventions JS References JS Versions

JS HTML DOM

JS HTML DOM

JS Events

JS Events

JS Projects

JS Projects

JS Advanced

JS Functions JS Objects JS Classes JS Asynchronous JS Modules JS Meta & Proxy JS Typed Arrays JS DOM Navigation JS Windows JS Web APIs JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


HTML DOM - Changing HTML

Changing HTML Content

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

The easiest way to modify the content is by using the innerHTML property:

document.getElementById(id).innerHTML = new HTML

The innerHTML Property

The easiest way to get the content of an element is by using the innerHTML property.

The innerHTML property is useful for getting or replacing the content of HTML elements.

Note

The innerHTML property can be used to get or change any HTML element, including <html> and <body>.


Common Mistakes

  • Trying to access a DOM element before it exists
  • Forgetting the quotes in an id like "demo"

Example

Change the content of a <p> element:

<html>
<body>

<p id="p1">Hello World!</p>

<script>
document.getElementById("p1").innerHTML = "New text!";
</script>

</body>
</html>
Try it Yourself »

Example explained:

  • The HTML document above contains a <p> element with id="p1"
  • We use the HTML DOM to get the element with id="p1"
  • A JavaScript changes the content (innerHTML) of that element to "New text!"

Example

Change the content of an <h1> element:

<!DOCTYPE html>
<html>
<body>

<h1 id="id01">Old Heading</h1>

<script>
const element = document.getElementById("id01");
element.innerHTML = "New Heading";
</script>

</body>
</html>
Try it Yourself »

Example explained:

  • The HTML document above contains an <h1> element with id="id01"
  • We use the HTML DOM to get the element with id="id01"
  • A JavaScript changes the content (innerHTML) of that element to "New Heading"


Changing an Attribute

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

document.getElementById(id).attribute = new value

Example

Changes the value of the src attribute of an <img> element:

<!DOCTYPE html>
<html>
<body>

<img id="myImage" src="smiley.gif">

<script>
document.getElementById("myImage").src = "landscape.jpg";
</script>

</body>
</html>
Try it Yourself »

Example explained:

  • The HTML document above contains an <img> element with id="myImage"
  • We use the HTML DOM to get the element with id="myImage"
  • A JavaScript changes the src attribute of that element from "smiley.gif" to "landscape.jpg"

Dynamic HTML content

JavaScript can create dynamic HTML content:

Example

<!DOCTYPE html>
<html>
<body>

<script>
document.getElementById("demo").innerHTML = "Date : " + Date();
</script>

</body>
</html>
Try it Yourself »

document.write()

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

Example

<!DOCTYPE html>
<html>
<body>

<p>Bla bla bla</p>

<script>
document.write(Date());
</script>

<p>Bla bla bla</p>

</body>
</html>
Try it Yourself »

Warning !

Never use document.write() after the document is loaded. It will overwrite the document.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->