Nearby lessons
5 of 14JavaScript 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"
Code Example
🔧 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"
Code Example
⚡ 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()