ST 4th Sem Bca
ST 4th Sem Bca
1. Introduction
The Document Object Model (DOM) allows developers to interact with and manipulate HTML and XML
documents programmatically. Using JavaScript and the DOM API, web developers can dynamically change
the structure, content, and style of a web page after it has been loaded into the browser.
DOM Manipulation refers to accessing, modifying, adding, or deleting elements, attributes, and
styles in an HTML document using JavaScript. This is crucial for creating dynamic and interactive
websites.
A. Accessing Elements
document.getElementById("id");
document.getElementsByClassName("class");
document.getElementsByTagName("tag");
document.querySelector(".class #id");
B. Changing Content
C. Changing Attributes
img.src = "new-image.jpg";
D. Changing Styles
document.getElementById("demo").style.color = "blue";
document.getElementById("box").style.backgroundColor = "yellow";
document.body.appendChild(para);
F. Removing Elements
element.remove(); // or
element.parentNode.removeChild(element);
G. Replacing Elements
oldEl.parentNode.replaceChild(newEl, oldEl);
document.getElementById("btn").addEventListener("click", function() {
alert("Button Clicked!");
});
6. Real-World Example
<script>
function changeText() {
</script>
Explanation: When the user clicks the button, JavaScript accesses the paragraph element and changes
its content dynamically using the DOM.
8. Conclusion
DOM manipulation is a core aspect of client-side web development, allowing developers to dynamically
modify and interact with documents. It enables responsive, user-friendly applications by changing
content, structure, and style in real time. Mastering DOM manipulation is essential for building
modern, interactive web applications.