{"id":8249,"date":"2025-07-24T21:32:40","date_gmt":"2025-07-24T21:32:39","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=8249"},"modified":"2025-07-24T21:32:40","modified_gmt":"2025-07-24T21:32:39","slug":"dom-manipulation-in-vanilla-javascript-12","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/dom-manipulation-in-vanilla-javascript-12\/","title":{"rendered":"DOM Manipulation in Vanilla JavaScript"},"content":{"rendered":"<h1>DOM Manipulation in Vanilla JavaScript<\/h1>\n<p>The Document Object Model (DOM) is a programming interface for web documents. It represents the structure of a document as a tree of objects, where each node corresponds to a part of the document (elements, attributes, text, etc.). Understanding DOM manipulation is essential for any web developer who seeks to create dynamic, interactive user experiences. In this blog post, we will explore DOM manipulation in Vanilla JavaScript, illustrating how to efficiently interact with and modify the contents of web pages.<\/p>\n<h2>What is DOM Manipulation?<\/h2>\n<p>DOM manipulation refers to the process of using JavaScript to change the structure, style, and content of a web page. This can include:<\/p>\n<ul>\n<li>Adding or removing elements<\/li>\n<li>Changing element attributes<\/li>\n<li>Modifying element content<\/li>\n<li>Responding to user events<\/li>\n<\/ul>\n<p>By manipulating the DOM, developers can create responsive and engaging applications that react to user actions in real-time.<\/p>\n<h2>Getting Started with Basic DOM Manipulation<\/h2>\n<p>To perform DOM manipulation with Vanilla JavaScript, we first need to understand how to select elements. The most commonly used methods for selecting DOM elements include:<\/p>\n<ul>\n<li><code>document.getElementById()<\/code><\/li>\n<li><code>document.getElementsByClassName()<\/code><\/li>\n<li><code>document.getElementsByTagName()<\/code><\/li>\n<li><code>document.querySelector()<\/code><\/li>\n<li><code>document.querySelectorAll()<\/code><\/li>\n<\/ul>\n<h3>Selecting Elements<\/h3>\n<p>Let\u2019s look at some examples of these selection methods:<\/p>\n<pre><code>const header = document.getElementById('header'); \/\/ Selects an element by ID\nconst items = document.getElementsByClassName('item'); \/\/ Selects elements by class name\nconst articles = document.getElementsByTagName('article'); \/\/ Selects elements by tag name\nconst firstItem = document.querySelector('.item'); \/\/ Selects the first element that matches the selector\nconst allItems = document.querySelectorAll('.item'); \/\/ Selects all elements that match the selector\n<\/code><\/pre>\n<h2>Creating and Modifying Elements<\/h2>\n<p>Once we&#8217;ve selected the elements we want to manipulate, we can create new elements, modify their existing properties, and append them to the DOM.<\/p>\n<h3>Creating Elements<\/h3>\n<p>To create a new DOM element, we use the <code>document.createElement()<\/code> method:<\/p>\n<pre><code>const newDiv = document.createElement('div'); \/\/ Creates a new div element\nnewDiv.textContent = 'Hello, World!'; \/\/ Adds text to the new element\nnewDiv.className = 'greeting'; \/\/ Assigns a class to the new element\n<\/code><\/pre>\n<h3>Appending Elements<\/h3>\n<p>After creating an element, we can append it to an existing element using <code>appendChild()<\/code>:<\/p>\n<pre><code>const container = document.getElementById('container'); \/\/ Selects the container\ncontainer.appendChild(newDiv); \/\/ Adds the new div as a child of the container\n<\/code><\/pre>\n<h3>Modifying Existing Elements<\/h3>\n<p>In addition to creating new elements, you can modify existing elements by changing their properties or attributes:<\/p>\n<pre><code>const item = document.querySelector('.item'); \/\/ Select an existing item\nitem.textContent = 'Updated Item'; \/\/ Change the text content\nitem.setAttribute('data-value', '123'); \/\/ Update an attribute\n<\/code><\/pre>\n<h2>Removing Elements<\/h2>\n<p>To remove an element from the DOM, you can use the <code>remove()<\/code> method or the <code>removeChild()<\/code> method:<\/p>\n<pre><code>const itemToDelete = document.querySelector('.item-to-delete');\nitemToDelete.remove(); \/\/ Removes the selected item directly\n\n\/\/ OR\n\nconst parent = itemToDelete.parentNode;\nparent.removeChild(itemToDelete); \/\/ Removes the selected item through its parent\n<\/code><\/pre>\n<h2>Manipulating Styles<\/h2>\n<p>DOM manipulation also extends to altering the styles of elements. This can be done using the <code>style<\/code> property:<\/p>\n<pre><code>const box = document.querySelector('.box');\nbox.style.backgroundColor = 'blue'; \/\/ Changes background color\nbox.style.color = 'white'; \/\/ Changes text color\nbox.style.padding = '10px'; \/\/ Adds padding\n<\/code><\/pre>\n<h2>Event Handling<\/h2>\n<p>Interactivity is a crucial aspect of modern web applications. JavaScript allows developers to handle events that occur in the DOM. This includes clicks, keystrokes, and form submissions.<\/p>\n<h3>Adding Event Listeners<\/h3>\n<p>To respond to events, you can use the <code>addEventListener()<\/code> method:<\/p>\n<pre><code>const button = document.getElementById('myButton');\nbutton.addEventListener('click', function() {\n    alert('Button was clicked!'); \/\/ Respond to the click event\n});\n<\/code><\/pre>\n<h2>Using Modern JavaScript Features<\/h2>\n<p>While working with the DOM in Vanilla JavaScript is straightforward, taking advantage of ES6 features can enhance readability and usability. Here are a few:<\/p>\n<h3>Arrow Functions<\/h3>\n<p>Arrow functions provide a cleaner syntax for defining functions:<\/p>\n<pre><code>button.addEventListener('click', () =&gt; {\n    console.log('Clicked using arrow function!');\n});\n<\/code><\/pre>\n<h3>Template Literals<\/h3>\n<p>Template literals make it easy to embed expressions into strings, useful for dynamic content:<\/p>\n<pre><code>const name = 'John';\nconst greeting = `Hello, ${name}!`; \/\/ Template literal usage\nnewDiv.textContent = greeting; \/\/ Set dynamic text using template literals\n<\/code><\/pre>\n<h3>Destructuring<\/h3>\n<p>JavaScript destructuring allows you to extract values from arrays or objects. It can be useful for simplifying your code:<\/p>\n<pre><code>const { style } = item; \/\/ Destructure the style property\nstyle.backgroundColor = 'red'; \/\/ Modify the background color directly\n<\/code><\/pre>\n<h2>Best Practices for DOM Manipulation<\/h2>\n<p>Efficient DOM manipulation is not only about knowledge but also about following best practices:<\/p>\n<ul>\n<li>Minimize DOM access: Accessing the DOM can be slow; cache references when possible.<\/li>\n<li>Batch updates: Make multiple changes before reflowing to the DOM to enhance performance.<\/li>\n<li>Use <code>DocumentFragment<\/code>: When adding multiple elements, create a fragment to minimize reflows.<\/li>\n<li>Avoid inline styles: Use CSS classes instead for better maintainability.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>Mastering DOM manipulation with Vanilla JavaScript is an indispensable skill in web development. By understanding how to select, create, modify, and remove elements, you can build more dynamic and interactive web applications. Using modern JavaScript features can further enhance your development experience, allowing for cleaner and more efficient code. Remember to adopt best practices to optimize performance and maintainability. <\/p>\n<p>Whether you&#8217;re a beginner or a seasoned developer, practicing these techniques will deepen your understanding of web technologies and empower you to create robust web solutions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>DOM Manipulation in Vanilla JavaScript The Document Object Model (DOM) is a programming interface for web documents. It represents the structure of a document as a tree of objects, where each node corresponds to a part of the document (elements, attributes, text, etc.). Understanding DOM manipulation is essential for any web developer who seeks to<\/p>\n","protected":false},"author":86,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[172],"tags":[330],"class_list":["post-8249","post","type-post","status-publish","format-standard","category-javascript","tag-javascript"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/86"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=8249"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8249\/revisions"}],"predecessor-version":[{"id":8250,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8249\/revisions\/8250"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=8249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=8249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=8249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}