🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to JavaScript Notes
Topic #143

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

<html>

<body>

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

<script>

document.getElementById("p1").innerHTML = "New text!";

</script>

</body>

</html>

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

<!DOCTYPE html>
<html>

<body>

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

<script>

const element = document.getElementById("id01");
element.innerHTML = "New Heading";

</script>

</body>

</html>

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

 <!DOCTYPE html>
<html>

<body>

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

<script>

document.getElementById("myImage").src = "landscape.jpg";

</script>

</body>

</html>

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>

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>

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

Want to go beyond the notes?

Join CodingNow 2.0's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Changing HTML – FAQs

Quick answers about learning Changing HTML in JavaScript.

This free note from CodingNow 2.0 explains Changing HTML in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on CodingNow 2.0, including Changing HTML, is 100% free with no signup required.
With focused practice, most students grasp Changing HTML in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now