Nearby lessons

5 of 14

JavaScript DOM – Changing HTML

📌 What is DOM HTML Manipulation?

JavaScript allows you to change HTML content dynamically. This makes your web pages interactive and engaging. You can update the content of elements, modify attributes, and even generate new HTML based on user actions or data.

📝 Changing HTML Content

You can use JavaScript to change the content of an HTML element using innerHTML or innerText. This is useful when you want to display updated information to the user.

Syntax:
element.innerHTML = "new content"
element.innerText = "new text"

Example02
🧩Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🔧 Changing the Value of an Attribute

JavaScript can also modify HTML element attributes such as src, href, value, etc. Use the setAttribute() method or change it directly.

Syntax:
element.setAttribute("attribute", "newValue")
element.attribute = "newValue"

Example03
🧩Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

⚡ Dynamic HTML Content

With JavaScript, you can create content on the fly—like adding new HTML elements dynamically based on data or user actions.

Syntax:
document.createElement()
appendChild()

Example04
🧩Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3