{"id":560,"date":"2025-05-19T10:52:05","date_gmt":"2025-05-19T10:52:05","guid":{"rendered":"https:\/\/buhave.com\/courses\/?p=560"},"modified":"2025-05-20T12:57:38","modified_gmt":"2025-05-20T12:57:38","slug":"dom-manipulation","status":"publish","type":"post","link":"https:\/\/buhave.com\/courses\/java-script\/dom-manipulation\/","title":{"rendered":"DOM Manipulation"},"content":{"rendered":"<h2>What is the DOM?<\/h2>\n<h3>DOM Meaning<\/h3>\n<p>The DOM (Document Object Model) is a programming interface that represents an HTML or XML document as a tree structure where each node is an object.<\/p>\n<p><strong>In simple words:<\/strong><\/p>\n<p>The DOM connects web pages to programming languages like JavaScript, allowing you to change the page dynamically.<\/p>\n<p><strong>Structure of the DOM<\/strong><\/p>\n<ul>\n<li>The whole web page becomes a tree of nodes.<\/li>\n<li>Each HTML element (like &lt;h1&gt;, &lt;p&gt;, &lt;div&gt;) is a node in the tree.<\/li>\n<li>There are different types of nodes:<\/li>\n<li>Document node (the root, entire page)<\/li>\n<li>Element nodes (tags like &lt;p&gt;, &lt;img&gt;)<\/li>\n<li>Text nodes (text inside elements)<\/li>\n<li>Attribute nodes (like class=&#8221;title&#8221;)<\/li>\n<\/ul>\n<p><strong>Example:<\/strong> Simple HTML Page and DOM<\/p>\n<p><strong>HTML:<\/strong><\/p>\n<p style=\"text-align: center\"><em>&lt;!DOCTYPE html&gt;<\/em><br \/>\n<em>&lt;html&gt;<\/em><br \/>\n<em>&lt;body&gt;<\/em><br \/>\n<em>&lt;h1&gt;Hello World&lt;\/h1&gt;<\/em><br \/>\n<em>&lt;p&gt;Welcome to the DOM.&lt;\/p&gt;<\/em><br \/>\n<em>&lt;\/body&gt;<\/em><br \/>\n<em>&lt;\/html&gt;<\/em><\/p>\n<p><strong>DOM Tree:<\/strong><\/p>\n<p style=\"text-align: center\"><em>Document<\/em><br \/>\n<em>\u2514\u2500\u2500 html<\/em><br \/>\n<em>\u2514\u2500\u2500 body<\/em><br \/>\n<em>\u251c\u2500\u2500 h1<\/em><br \/>\n<em>\u2502 \u2514\u2500\u2500 &#8220;Hello World&#8221;<\/em><br \/>\n<em>\u2514\u2500\u2500 p<\/em><br \/>\n<em>\u2514\u2500\u2500 &#8220;Welcome to the DOM.&#8221;<\/em><\/p>\n<p>The DOM turns HTML into a structure that JavaScript can read and manipulate.<\/p>\n<h3>How JavaScript Interacts with the DOM<\/h3>\n<p><strong>You can use JavaScript to:<\/strong><\/p>\n<ul>\n<li>Select elements (getElementById, querySelector, etc.)<\/li>\n<li>Change content (element.textContent, element.innerHTML)<\/li>\n<li>Change styles (element.style.color = &#8220;red&#8221;)<\/li>\n<li>Add or remove elements (appendChild, removeChild)<\/li>\n<li>Handle events (clicks, typing, scrolling)<\/li>\n<\/ul>\n<p><strong>Example:<\/strong> Changing Text Using the DOM<\/p>\n<p style=\"text-align: center\"><em>&lt;h1 id=&#8221;title&#8221;&gt;Hello&lt;\/h1&gt;<\/em><\/p>\n<p style=\"text-align: center\"><em>&lt;script&gt;<\/em><br \/>\n<em>const heading = document.getElementById(&#8220;title&#8221;);<\/em><br \/>\n<em>heading.textContent = &#8220;Hello, DOM World!&#8221;;<\/em><br \/>\n<em>&lt;\/script&gt;<\/em><\/p>\n<p>The &lt;h1&gt; content changes when the script runs.<\/p>\n<p><strong>Why the DOM is Important<\/strong><\/p>\n<ul>\n<li>Dynamic updates without reloading the page.<\/li>\n<li>Interactive websites (like buttons, forms, pop-ups).<\/li>\n<li>Control over structure, style, and content of a webpage.<\/li>\n<\/ul>\n<p>Without the DOM, JavaScript could not manipulate HTML and webpages would be static.<\/p>\n<p><strong>Quick Summary:<\/strong><\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n<thead>\n<tr>\n<th>Topic<\/th>\n<th>Explanation<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>DOM Definition<\/td>\n<td>Interface that represents HTML\/XML pages as trees<\/td>\n<\/tr>\n<tr>\n<td>DOM Purpose<\/td>\n<td>Connects web pages with programming languages<\/td>\n<\/tr>\n<tr>\n<td>DOM Structure<\/td>\n<td>Tree with nodes (Document, Element, Text, Attribute)<\/td>\n<\/tr>\n<tr>\n<td>JavaScript Role<\/td>\n<td>Access and change elements dynamically<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Selecting elements (getElementById, querySelector, etc.)<\/h2>\n<p><strong>Why Select Elements?<\/strong><\/p>\n<p>Selecting elements means grabbing HTML elements using JavaScript so you can read, change, or manipulate them.<br \/>\nYou can modify text, style, attributes, or even react to user actions like clicks.<\/p>\n<h3>Common Methods to Select Elements<\/h3>\n<p><strong>1. GetElementById()<\/strong><\/p>\n<ul>\n<li>Purpose: Selects one element with a specific id.<\/li>\n<li>Returns: A single element (or null if not found).<\/li>\n<li>Fastest method for selecting.<\/li>\n<\/ul>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>const element = document.getElementById(&#8220;elementId&#8221;);<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>&lt;h1 id=&#8221;main-title&#8221;&gt;Welcome!&lt;\/h1&gt;<\/em><\/p>\n<p style=\"text-align: center\"><em>const title = document.getElementById(&#8220;main-title&#8221;);<\/em><br \/>\n<em>console.log(title.textContent); \/\/ &#8220;Welcome!&#8221;<\/em><\/p>\n<p><strong data-start=\"931\" data-end=\"939\">Tip:<\/strong> IDs must be unique on a page.<\/p>\n<p><strong>2. getElementsByClassName()<\/strong><\/p>\n<ul>\n<li>Purpose: Selects all elements with a specific class name.<\/li>\n<li>Returns: An HTMLCollection (looks like an array but not exactly).<\/li>\n<\/ul>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>const elements = document.getElementsByClassName(&#8220;className&#8221;);<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>&lt;p class=&#8221;info&#8221;&gt;Info 1&lt;\/p&gt;<\/em><br \/>\n<em>&lt;p class=&#8221;info&#8221;&gt;Info 2&lt;\/p&gt;<\/em><\/p>\n<p style=\"text-align: center\"><em>const infoItems = document.getElementsByClassName(&#8220;info&#8221;);<\/em><br \/>\n<em>console.log(infoItems.length); \/\/ 2<\/em><\/p>\n<p>Access with index infoItems[0], infoItems[1].<\/p>\n<p><strong>3. getElementsByTagName()<\/strong><\/p>\n<ul>\n<li>Purpose: Selects all elements of a specific tag (like p, div, h1).<\/li>\n<li>Returns: An HTMLCollection.<\/li>\n<\/ul>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>const paragraphs = document.getElementsByTagName(&#8220;p&#8221;);<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>&lt;p&gt;Paragraph 1&lt;\/p&gt;<\/em><br \/>\n<em>&lt;p&gt;Paragraph 2&lt;\/p&gt;<\/em><\/p>\n<p style=\"text-align: center\"><em>const allParagraphs = document.getElementsByTagName(&#8220;p&#8221;);<\/em><br \/>\n<em>console.log(allParagraphs.length); \/\/ 2<\/em><\/p>\n<p><strong>4. querySelector()<\/strong><\/p>\n<ul>\n<li>Purpose: Selects the first element that matches a CSS selector.<\/li>\n<li>Returns: A single element (or null if none found).<\/li>\n<li>Very powerful and flexible.<\/li>\n<\/ul>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>const element = document.querySelector(&#8220;selector&#8221;);<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>&lt;div class=&#8221;box&#8221;&gt;Box 1&lt;\/div&gt;<\/em><br \/>\n<em>&lt;div class=&#8221;box&#8221;&gt;Box 2&lt;\/div&gt;<\/em><\/p>\n<p style=\"text-align: center\"><em>const firstBox = document.querySelector(&#8220;.box&#8221;);<\/em><br \/>\n<em>console.log(firstBox.textContent); \/\/ &#8220;Box 1&#8221;<\/em><\/p>\n<p><strong>CSS Selectors:<\/strong> IDs (#id), classes (.class), tags (div), attributes ([type=&#8221;text&#8221;]).<\/p>\n<p><strong>5. querySelectorAll()<\/strong><\/p>\n<ul>\n<li>Purpose: Selects all elements that match a CSS selector.<\/li>\n<li>Returns: A NodeList (can use forEach).<\/li>\n<\/ul>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\">c<em>onst elements = document.querySelectorAll(&#8220;selector&#8221;);<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>&lt;p class=&#8221;highlight&#8221;&gt;First&lt;\/p&gt;<\/em><br \/>\n<em>&lt;p class=&#8221;highlight&#8221;&gt;Second&lt;\/p&gt;<\/em><\/p>\n<p style=\"text-align: center\"><em>const highlights = document.querySelectorAll(&#8220;.highlight&#8221;);<\/em><br \/>\n<em>highlights.forEach(item =&gt; console.log(item.textContent));<\/em><\/p>\n<p><strong>HTMLCollection vs. NodeList<\/strong><\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n<thead>\n<tr>\n<th>Feature<\/th>\n<th>HTMLCollection<\/th>\n<th>NodeList<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Returned by<\/td>\n<td>getElementsByClassName, getElementsByTagName<\/td>\n<td>querySelectorAll<\/td>\n<\/tr>\n<tr>\n<td>Live Update<\/td>\n<td>Yes (updates if DOM changes)<\/td>\n<td>No (static snapshot)<\/td>\n<\/tr>\n<tr>\n<td>forEach() Method<\/td>\n<td>(needs conversion)<\/td>\n<td>(direct use)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Choosing the Right Method<\/strong><\/p>\n<table style=\"width: 100%;border-collapse: collapse;font-family: Arial, sans-serif\" border=\"1\" cellspacing=\"0\" cellpadding=\"8\">\n<thead style=\"background-color: #f2f2f2\">\n<tr>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #ddd\">Situation<\/th>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #ddd\">Best Method<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"padding: 12px;border-bottom: 1px solid #ddd\">Need single element by ID<\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #ddd\"><code>getElementById()<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 12px;border-bottom: 1px solid #ddd\">Need elements by class name or tag<\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #ddd\"><code>getElementsByClassName()<\/code>,<br \/>\n<code>getElementsByTagName()<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 12px;border-bottom: 1px solid #ddd\">Need first match by CSS selector<\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #ddd\"><code>querySelector()<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 12px\">Need all matches by CSS selector<\/td>\n<td style=\"padding: 12px\"><code>querySelectorAll()<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Real Example &#8211; Full Flow<\/strong><\/p>\n<p><strong>HTML:<\/strong><\/p>\n<p style=\"text-align: center\"><em>&lt;h1 id=&#8221;site-title&#8221;&gt;Hello&lt;\/h1&gt;<\/em><br \/>\n<em>&lt;p class=&#8221;description&#8221;&gt;First paragraph.&lt;\/p&gt;<\/em><br \/>\n<em>&lt;p class=&#8221;description&#8221;&gt;Second paragraph.&lt;\/p&gt;<\/em><\/p>\n<p><strong>JavaScript:<\/strong><\/p>\n<p style=\"text-align: center\"><em>const title = document.getElementById(&#8220;site-title&#8221;);<\/em><br \/>\n<em>const descriptions = document.getElementsByClassName(&#8220;description&#8221;);<\/em><br \/>\n<em>const firstParagraph = document.querySelector(&#8220;.description&#8221;);<\/em><br \/>\n<em>const allParagraphs = document.querySelectorAll(&#8220;.description&#8221;);<\/em><\/p>\n<p style=\"text-align: center\"><em>console.log(title.textContent); \/\/ Hello<\/em><br \/>\n<em>console.log(descriptions.length); \/\/ 2<\/em><br \/>\n<em>console.log(firstParagraph.textContent); \/\/ First paragraph.<\/em><br \/>\n<em>allParagraphs.forEach(p =&gt; console.log(p.textContent));<\/em><\/p>\n<p><strong>Quick Summary:<\/strong><\/p>\n<p>In JavaScript, you can select elements using methods like getElementById, querySelector, getElementsByClassName, and querySelectorAll to manipulate or interact with the page easily.<\/p>\n<h2>Changing content and styles<\/h2>\n<p><strong>Why Change Content and Styles?<\/strong><\/p>\n<p>Using JavaScript, you can dynamically update:<\/p>\n<ul>\n<li>Text<\/li>\n<li>HTML structure<\/li>\n<li>CSS styles<\/li>\n<\/ul>\n<p>This makes websites interactive, dynamic, and user-friendly \u2014 no page reload needed!<\/p>\n<h3>Changing Content<\/h3>\n<p><strong>There are several ways to change the content of HTML elements:<\/strong><\/p>\n<p><strong>1. Text Content<\/strong><\/p>\n<ul>\n<li>Sets or gets the text inside an element.<\/li>\n<li>Ignores HTML tags, treats everything as plain text.<\/li>\n<\/ul>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>element.textContent = &#8220;New Text Here&#8221;;<\/em><\/p>\n<p><strong>HTML:<\/strong><\/p>\n<p style=\"text-align: center\"><em>&lt;p id=&#8221;greet&#8221;&gt;Hello!&lt;\/p&gt;<\/em><\/p>\n<p><strong>JavaScript:<\/strong><\/p>\n<p style=\"text-align: center\"><em>const greet = document.getElementById(&#8220;greet&#8221;);<\/em><br \/>\n<em>greet.textContent = &#8220;Welcome to the site!&#8221;;<\/em><\/p>\n<p><strong>Result:<\/strong> Only text is inserted.<\/p>\n<p><strong>2. Inner HTML<\/strong><\/p>\n<ul>\n<li>Sets or gets the HTML content inside an element.<\/li>\n<li>Allows HTML tags inside.<\/li>\n<\/ul>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>element.innerHTML = &#8220;&lt;strong&gt;Bold Text&lt;\/strong&gt;&#8221;;<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>&lt;div id=&#8221;content&#8221;&gt;&lt;\/div&gt;<\/em><\/p>\n<p><strong>JavaScript:<\/strong><\/p>\n<p style=\"text-align: center\"><em>const content = document.getElementById(&#8220;content&#8221;);<\/em><br \/>\n<em>content.innerHTML = &#8220;&lt;h2&gt;New Heading&lt;\/h2&gt;&lt;p&gt;New paragraph!&lt;\/p&gt;&#8221;;<\/em><\/p>\n<ul>\n<li><strong>Result:<\/strong> HTML structure is rendered inside the div.<\/li>\n<li><strong>Warning:<\/strong> Using innerHTML can open security risks (like XSS attacks) if you insert untrusted content.<\/li>\n<\/ul>\n<h2>Handling events (click, input, submit)<\/h2>\n<p><strong>Handling Events in JavaScript<\/strong><\/p>\n<p>JavaScript allows you to handle user interactions (events) like clicks, inputs, and form submissions to make web pages dynamic. Events are triggered by user actions, such as clicking a button or typing in a text box, and JavaScript listens for these events to execute a function.<\/p>\n<h3>There are many types of Handling Events in JavaScript.<\/h3>\n<p><strong>1. Event Types<\/strong><\/p>\n<ul>\n<li>Click: Triggered when a user clicks an element (e.g., button, link).<\/li>\n<li>Input: Triggered when a user types into a text field or textarea.<\/li>\n<li>Submit: Triggered when a user submits a form.<\/li>\n<\/ul>\n<p><strong>2. Attaching Event Listeners<\/strong><\/p>\n<p><strong>Method 1:<\/strong> Using addEventListener()<\/p>\n<p>The addEventListener() method attaches an event handler to an element. It\u2019s flexible and allows multiple event listeners for the same event.<\/p>\n<p><strong>Example &#8211; Handling a Click Event:<\/strong><\/p>\n<p style=\"text-align: center\"><em>&lt;button id=&#8221;myButton&#8221;&gt;Click Me&lt;\/button&gt;<\/em><\/p>\n<p style=\"text-align: center\"><em>&lt;script&gt;<\/em><br \/>\n<em>const button = document.getElementById(&#8216;myButton&#8217;);<\/em><br \/>\n<em>button.addEventListener(&#8216;click&#8217;, function() {<\/em><br \/>\n<em>alert(&#8216;Button clicked!&#8217;);<\/em><br \/>\n<em>});<\/em><br \/>\n<em>&lt;\/script&gt;<\/em><\/p>\n<p><strong>Example &#8211; Handling Input Event:<\/strong><\/p>\n<p style=\"text-align: center\"><em>&lt;input type=&#8221;text&#8221; id=&#8221;myInput&#8221; placeholder=&#8221;Type something&#8221;&gt;<\/em><\/p>\n<p style=\"text-align: center\"><em>&lt;script&gt;<\/em><br \/>\n<em>const input = document.getElementById(&#8216;myInput&#8217;);<\/em><br \/>\n<em>input.addEventListener(&#8216;input&#8217;, function() {<\/em><br \/>\n<em>console.log(&#8216;User typed: &#8216; + input.value);<\/em><br \/>\n<em>});<\/em><br \/>\n<em>&lt;\/script&gt;<\/em><\/p>\n<p><strong>Example &#8211; Handling Submit Event:<\/strong><\/p>\n<p style=\"text-align: center\"><em>&lt;form id=&#8221;myForm&#8221;&gt;<\/em><br \/>\n<em>&lt;input type=&#8221;text&#8221; name=&#8221;name&#8221;&gt;<\/em><br \/>\n<em>&lt;button type=&#8221;submit&#8221;&gt;Submit&lt;\/button&gt;<\/em><br \/>\n<em>&lt;\/form&gt;<\/em><\/p>\n<p style=\"text-align: center\"><em>&lt;script&gt;<\/em><br \/>\n<em>const form = document.getElementById(&#8216;myForm&#8217;);<\/em><br \/>\n<em>form.addEventListener(&#8216;submit&#8217;, function(event) {<\/em><br \/>\n<em>event.preventDefault(); \/\/ Prevent default form submission<\/em><br \/>\n<em>console.log(&#8216;Form submitted!&#8217;);<\/em><br \/>\n<em>});<\/em><br \/>\n<em>&lt;\/script&gt;<\/em><\/p>\n<p><strong>3. Event Propagation (Bubbling and Capturing)<\/strong><\/p>\n<ul>\n<li>Event Bubbling: Events propagate from the target element up to the root.<\/li>\n<li>Event Capturing: Events propagate from the root to the target element.<\/li>\n<\/ul>\n<p>You can control propagation with stopPropagation() and stopImmediatePropagation().<\/p>\n<p style=\"text-align: center\"><em>button.addEventListener(&#8216;click&#8217;, function(event) {<\/em><br \/>\n<em>event.stopPropagation(); \/\/ Prevents the event from bubbling up<\/em><br \/>\n<em>});<\/em><\/p>\n<p><strong>4. Handling Multiple Events on the Same Element<\/strong><\/p>\n<p>You can use addEventListener() for multiple events. For instance, to handle both click and input.<\/p>\n<p style=\"text-align: center\"><em>element.addEventListener(&#8216;click&#8217;, function() {<\/em><br \/>\n<em>console.log(&#8216;Clicked!&#8217;);<\/em><br \/>\n<em>});<\/em><\/p>\n<p style=\"text-align: center\"><em>element.addEventListener(&#8216;input&#8217;, function() {<\/em><br \/>\n<em>console.log(&#8216;Input changed!&#8217;);<\/em><br \/>\n<em>});<\/em><\/p>\n<p><strong>5. Event Object<\/strong><\/p>\n<p>Every event handler receives an event object, which contains useful information like the target element, event type, and more.<\/p>\n<p style=\"text-align: center\"><em>button.addEventListener(&#8216;click&#8217;, function(event) {<\/em><br \/>\n<em>console.log(event.target); \/\/ Element that triggered the event<\/em><br \/>\n<em>console.log(event.type); \/\/ Event type (e.g., &#8216;click&#8217;)<\/em><br \/>\n<em>});<\/em><\/p>\n<p><strong>Summary:<\/strong><\/p>\n<p>Event handling in JavaScript allows developers to create interactive web pages by listening for user actions (e.g., clicks, inputs, and submits) and responding with custom functions. Methods like addEventListener() provide flexibility, while event propagation and the event object offer control and detailed information about the event.<\/p>\n<h2>Creating and Removing Elements Dynamically<\/h2>\n<p>JavaScript allows you to create, insert, modify, and remove HTML elements dynamically, enabling you to build highly interactive and real-time updating web pages.<\/p>\n<h3>Creating Elements<\/h3>\n<p><strong>You can create a new HTML element using document.createElement():<\/strong><\/p>\n<p style=\"text-align: center\"><em>const newDiv = document.createElement(&#8216;div&#8217;); \/\/ Create a new &lt;div&gt; element<\/em><br \/>\n<em>newDiv.textContent = &#8216;Hello, World!&#8217;; \/\/ Add text inside the div<\/em><br \/>\n<em>newDiv.classList.add(&#8216;greeting&#8217;); \/\/ Add a class<\/em><br \/>\n<em>document.body.appendChild(newDiv); \/\/ Append it to the body<\/em><\/p>\n<p><strong>Key Methods:<\/strong><\/p>\n<ul>\n<li>document.createElement(tagName): Creates a new element.<\/li>\n<li>element.textContent = &#8216;&#8230;&#8217;: Adds text inside an element.<\/li>\n<li>element.classList.add(className): Adds CSS classes.<\/li>\n<li>element.setAttribute(name, value): Sets attributes like id, href, etc.<\/li>\n<li>parent.appendChild(element): Inserts the new element into the DOM.<\/li>\n<\/ul>\n<p><strong>Inserting at Specific Positions<\/strong><\/p>\n<ul>\n<li>parent.appendChild(child): Adds at the end.<\/li>\n<li>parent.prepend(child): Adds at the beginning.<\/li>\n<li>parent.insertBefore(newNode, referenceNode): Inserts before a specified node.<\/li>\n<\/ul>\n<p style=\"text-align: center\"><em>const referenceNode = document.getElementById(&#8216;reference&#8217;);<\/em><br \/>\n<em>document.body.insertBefore(newDiv, referenceNode);<\/em><\/p>\n<h3>Removing Elements<\/h3>\n<p><strong>To remove an element:<\/strong><\/p>\n<ul>\n<li>Modern way: element.remove()<\/li>\n<li>Old way: parent.removeChild(child)<\/li>\n<\/ul>\n<p style=\"text-align: center\"><em>const element = document.getElementById(&#8216;toRemove&#8217;);<\/em><br \/>\n<em>element.remove(); \/\/ Direct removal<\/em><\/p>\n<p><strong>Or:<\/strong><\/p>\n<p style=\"text-align: center\"><em>const parent = document.getElementById(&#8216;parent&#8217;);<\/em><br \/>\n<em>const child = document.getElementById(&#8216;child&#8217;);<\/em><br \/>\n<em>parent.removeChild(child);<\/em><\/p>\n<p><strong>Replacing Elements<\/strong><\/p>\n<p><strong>Use replaceChild(newNode, oldNode):<\/strong><\/p>\n<p style=\"text-align: center\"><em>const newHeading = document.createElement(&#8216;h2&#8217;);<\/em><br \/>\n<em>newHeading.textContent = &#8216;New Heading&#8217;;<\/em><br \/>\n<em>document.body.replaceChild(newHeading, document.getElementById(&#8216;oldHeading&#8217;));<\/em><\/p>\n<p><strong>Quick Summary:<\/strong><\/p>\n<p>In JavaScript, you can create elements with createElement(), insert them with appendChild() or prepend(), remove them using remove() or removeChild(), and replace existing elements with replaceChild(), giving you full control over the page structure dynamically.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is the DOM? DOM Meaning The DOM (Document Object Model) is a programming interface that represents an HTML or XML document as a tree structure where each node is an object. In simple words: The DOM connects web pages to programming languages like JavaScript,<\/p>\n","protected":false},"author":1,"featured_media":562,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-560","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-script"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>DOM Manipulation - Java Script Course<\/title>\n<meta name=\"description\" content=\"DOM Manipulation means dynamically creating, editing, and removing HTML elements to update a web page instantly.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/buhave.com\/courses\/java-script\/dom-manipulation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"DOM Manipulation - Java Script Course\" \/>\n<meta property=\"og:description\" content=\"DOM Manipulation means dynamically creating, editing, and removing HTML elements to update a web page instantly.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/buhave.com\/courses\/java-script\/dom-manipulation\/\" \/>\n<meta property=\"og:site_name\" content=\"BUHAVE\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/BeYouHave\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/naveedsafdarawan\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-19T10:52:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-20T12:57:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/DOM-Manipulation.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Naveed Safdar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Naveed Safdar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/dom-manipulation\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/dom-manipulation\\\/\"},\"author\":{\"name\":\"Naveed Safdar\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\"},\"headline\":\"DOM Manipulation\",\"datePublished\":\"2025-05-19T10:52:05+00:00\",\"dateModified\":\"2025-05-20T12:57:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/dom-manipulation\\\/\"},\"wordCount\":1691,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/dom-manipulation\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/DOM-Manipulation.webp\",\"articleSection\":[\"Java Script Course\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/dom-manipulation\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/dom-manipulation\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/dom-manipulation\\\/\",\"name\":\"DOM Manipulation - Java Script Course\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/dom-manipulation\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/dom-manipulation\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/DOM-Manipulation.webp\",\"datePublished\":\"2025-05-19T10:52:05+00:00\",\"dateModified\":\"2025-05-20T12:57:38+00:00\",\"description\":\"DOM Manipulation means dynamically creating, editing, and removing HTML elements to update a web page instantly.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/dom-manipulation\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/dom-manipulation\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/dom-manipulation\\\/#primaryimage\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/DOM-Manipulation.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/DOM-Manipulation.webp\",\"width\":1200,\"height\":628,\"caption\":\"DOM Manipulation\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/dom-manipulation\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Courses\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Script Course\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/learn\\\/java-script\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"DOM Manipulation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"name\":\"BUHAVE\",\"description\":\"Courses - Learn Online for Free\",\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/buhave.com\\\/courses\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\",\"name\":\"BUHAVE\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"width\":375,\"height\":75,\"caption\":\"BUHAVE\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/BeYouHave\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/buhave\",\"https:\\\/\\\/www.youtube.com\\\/@buhave\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\",\"name\":\"Naveed Safdar\",\"description\":\"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/naveedsafdar\\\/\",\"https:\\\/\\\/www.facebook.com\\\/naveedsafdarawan\\\/\",\"https:\\\/\\\/www.youtube.com\\\/@naveedsafdar\"],\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/author\\\/naveed-safdar\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"DOM Manipulation - Java Script Course","description":"DOM Manipulation means dynamically creating, editing, and removing HTML elements to update a web page instantly.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/buhave.com\/courses\/java-script\/dom-manipulation\/","og_locale":"en_US","og_type":"article","og_title":"DOM Manipulation - Java Script Course","og_description":"DOM Manipulation means dynamically creating, editing, and removing HTML elements to update a web page instantly.","og_url":"https:\/\/buhave.com\/courses\/java-script\/dom-manipulation\/","og_site_name":"BUHAVE","article_publisher":"https:\/\/www.facebook.com\/BeYouHave\/","article_author":"https:\/\/www.facebook.com\/naveedsafdarawan\/","article_published_time":"2025-05-19T10:52:05+00:00","article_modified_time":"2025-05-20T12:57:38+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/DOM-Manipulation.webp","type":"image\/webp"}],"author":"Naveed Safdar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Naveed Safdar","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/buhave.com\/courses\/java-script\/dom-manipulation\/#article","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/java-script\/dom-manipulation\/"},"author":{"name":"Naveed Safdar","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca"},"headline":"DOM Manipulation","datePublished":"2025-05-19T10:52:05+00:00","dateModified":"2025-05-20T12:57:38+00:00","mainEntityOfPage":{"@id":"https:\/\/buhave.com\/courses\/java-script\/dom-manipulation\/"},"wordCount":1691,"commentCount":0,"publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"image":{"@id":"https:\/\/buhave.com\/courses\/java-script\/dom-manipulation\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/DOM-Manipulation.webp","articleSection":["Java Script Course"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/buhave.com\/courses\/java-script\/dom-manipulation\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/buhave.com\/courses\/java-script\/dom-manipulation\/","url":"https:\/\/buhave.com\/courses\/java-script\/dom-manipulation\/","name":"DOM Manipulation - Java Script Course","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/#website"},"primaryImageOfPage":{"@id":"https:\/\/buhave.com\/courses\/java-script\/dom-manipulation\/#primaryimage"},"image":{"@id":"https:\/\/buhave.com\/courses\/java-script\/dom-manipulation\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/DOM-Manipulation.webp","datePublished":"2025-05-19T10:52:05+00:00","dateModified":"2025-05-20T12:57:38+00:00","description":"DOM Manipulation means dynamically creating, editing, and removing HTML elements to update a web page instantly.","breadcrumb":{"@id":"https:\/\/buhave.com\/courses\/java-script\/dom-manipulation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/buhave.com\/courses\/java-script\/dom-manipulation\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/java-script\/dom-manipulation\/#primaryimage","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/DOM-Manipulation.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/DOM-Manipulation.webp","width":1200,"height":628,"caption":"DOM Manipulation"},{"@type":"BreadcrumbList","@id":"https:\/\/buhave.com\/courses\/java-script\/dom-manipulation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Courses","item":"https:\/\/buhave.com\/courses\/"},{"@type":"ListItem","position":2,"name":"Java Script Course","item":"https:\/\/buhave.com\/courses\/learn\/java-script\/"},{"@type":"ListItem","position":3,"name":"DOM Manipulation"}]},{"@type":"WebSite","@id":"https:\/\/buhave.com\/courses\/#website","url":"https:\/\/buhave.com\/courses\/","name":"BUHAVE","description":"Courses - Learn Online for Free","publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/buhave.com\/courses\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/buhave.com\/courses\/#organization","name":"BUHAVE","url":"https:\/\/buhave.com\/courses\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","width":375,"height":75,"caption":"BUHAVE"},"image":{"@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/BeYouHave\/","https:\/\/www.linkedin.com\/company\/buhave","https:\/\/www.youtube.com\/@buhave"]},{"@type":"Person","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca","name":"Naveed Safdar","description":"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.","sameAs":["https:\/\/www.linkedin.com\/in\/naveedsafdar\/","https:\/\/www.facebook.com\/naveedsafdarawan\/","https:\/\/www.youtube.com\/@naveedsafdar"],"url":"https:\/\/buhave.com\/courses\/author\/naveed-safdar\/"}]}},"_links":{"self":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/comments?post=560"}],"version-history":[{"count":1,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/560\/revisions"}],"predecessor-version":[{"id":563,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/560\/revisions\/563"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media\/562"}],"wp:attachment":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media?parent=560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/categories?post=560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/tags?post=560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}