<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Jonathan R. Mugisha</title>
    <description>The latest articles on Forem by Jonathan R. Mugisha (@jroycodes).</description>
    <link>https://forem.com/jroycodes</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F927248%2F6b8759dc-f826-4c83-9c2b-a9f012286eb9.jpeg</url>
      <title>Forem: Jonathan R. Mugisha</title>
      <link>https://forem.com/jroycodes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jroycodes"/>
    <language>en</language>
    <item>
      <title>How To Get Started With "DOM" in JavaScript.</title>
      <dc:creator>Jonathan R. Mugisha</dc:creator>
      <pubDate>Mon, 15 Apr 2024 17:41:06 +0000</pubDate>
      <link>https://forem.com/jroycodes/how-to-get-started-with-dom-in-javascript-pg7</link>
      <guid>https://forem.com/jroycodes/how-to-get-started-with-dom-in-javascript-pg7</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this article I will explain what the JavaScript DOM (Document Object Model) is and at the end of this article you might even be able to manipulate it.&lt;/p&gt;

&lt;p&gt;Lets get into it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the DOM?
&lt;/h2&gt;

&lt;p&gt;The Document Object Model is a tree-like representation of the the structure and contents of a webpage. Just like a family tree.&lt;/p&gt;

&lt;p&gt;We call a data structure a tree when it has a branching structure and a single, well-defined root. In the case of the DOM, the document serves as the root node.&lt;/p&gt;

&lt;p&gt;And just as any other tree has nodes, the same goes for the DOM. And we call these Elements, which represent HTML tags and determine the structure of the document. Other elements like the head and body tags can also serve as parent nodes to their own child nodes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is it important?
&lt;/h2&gt;

&lt;p&gt;The DOM serves as the backbone of every web page. It provides a structured representation of all the components and content of HTML or XML documents which allows developers to navigate, access and manipulate them. It also allows web developers the ability to create dynamic and interactive content for users.&lt;/p&gt;

&lt;h2&gt;
  
  
  Targeting nodes.
&lt;/h2&gt;

&lt;p&gt;When working with the DOM, we use " selectors " to target the nodes we want to work with. You can use a combination of CSS-style selectors and relationship properties to target the nodes.&lt;/p&gt;

&lt;p&gt;You can also use relational selectors (&lt;code&gt;firstElementChild&lt;/code&gt; or &lt;code&gt;lastElementChild&lt;/code&gt;) with special properties owned by the nodes.&lt;/p&gt;

&lt;h2&gt;
  
  
  DOM methods
&lt;/h2&gt;

&lt;p&gt;DOM methods are functions provided by the DOM interface that allow us to access and manipulate HTML elements on web pages using JavaScript. Here are some examples.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;querySelector()&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;querySelector()&lt;/code&gt; selects the first element that matches a given CSS selector.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const element = document.querySelector("#elementId");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;querySelectorAll()&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;querySelectorAll()&lt;/code&gt; selects all matching elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const element = document.querySelectorAll(".className");

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;getElementById()&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This method selects based on the &lt;code&gt;id&lt;/code&gt; attribute of an element on the page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const element = document.querySelector("elementID");

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;getElementsByClassName()&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This method is used to select all elements belonging to a particular &lt;code&gt;class&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const element = document.getElementsByClassName("className");

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;getElementsByTagName()&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This method is used to select all elements with a specific &lt;code&gt;tag&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const element = document.getElementsByTagName("div");

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  DOM manipulation
&lt;/h2&gt;

&lt;p&gt;This refers to the process of dynamically modifying the structure, changing page content, adding new elements, removing existing elements, and updating style properties.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a new Element :
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;document.createElement(tagName, [options])&lt;/strong&gt; - creates a new element of tag type tagName. [options] in this case means you can add some optional parameters to the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const newElement = document.createElement("div");

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;📌 This function does NOT put your new element into the DOM - it creates it in memory.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is so that you can manipulate the element (by adding styles, classes, ids, text, etc.) before placing it on the page.&lt;/p&gt;

&lt;p&gt;You can place the element into the DOM with one of the following methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. parentNode.appendChild(childNode) 

2. parentNode.insertBefore(newNode, referenceNode)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Removing Elements :
&lt;/h3&gt;

&lt;p&gt;To remove an existing element, we must find the parent element of the element and then use the &lt;code&gt;removeChild()&lt;/code&gt;method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const removeElement = document.getElementById("elementId");
removeElement.parentNode.removeChild(removeElement);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Changing Text Content :
&lt;/h3&gt;

&lt;p&gt;We can use textContent or innerHTML properties to change the text content of an element within the DOM.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const element = document.getElementById("myelementId");
element.textContent = "Hello World! ";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;💡 Note that using &lt;code&gt;textContent&lt;/code&gt; is preferred over &lt;code&gt;innerHTML&lt;/code&gt; for adding text.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Altering Elements :
&lt;/h3&gt;

&lt;p&gt;We can change the CSS style properties and other properties of the elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const element = document.getElementById("elementId");
element.style.color = "red";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep in mind that the JavaScript does not alter your HTML, but the DOM - your HTML file will look the same, but the JavaScript changes what the browser renders.&lt;/p&gt;

&lt;p&gt;You can inspect the DOM structure of a web page using your browser's developer tool.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Open Developer Tools:&lt;/strong&gt; Right-click on the element you want to inspect and select "Inspect" or press &lt;code&gt;Ctrl + Shift + I&lt;/code&gt; (or &lt;code&gt;Cmd + Option + I&lt;/code&gt; on macOS) to open the Developer Tools panel.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Inspect Element:&lt;/strong&gt; Click on any element in the DOM inspector to view its properties, styles, and event listeners in the browser's developer tools panel.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fth3ueb9wt76xe1d6hgtq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fth3ueb9wt76xe1d6hgtq.png" alt="Image description" width="800" height="265"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;DOM is a fundamental concept in the world of modern web development. Get manipulating, you're good to go.&lt;/p&gt;

&lt;p&gt;Found this blog post helpful? Consider sharing it with others who might benefit as well.&lt;/p&gt;

&lt;p&gt;If you have questions or any feedback please let me know in the comments. It will help me improve this and more blogs to come.&lt;/p&gt;

&lt;p&gt;Thanks for reading and see you in the next one!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>dom</category>
    </item>
    <item>
      <title>New Beginnings</title>
      <dc:creator>Jonathan R. Mugisha</dc:creator>
      <pubDate>Wed, 10 Apr 2024 19:11:46 +0000</pubDate>
      <link>https://forem.com/jroycodes/new-beginnings-3bf7</link>
      <guid>https://forem.com/jroycodes/new-beginnings-3bf7</guid>
      <description>&lt;p&gt;Hello world!👋🏾 and welcome to my first article. I created this blog with the hope that I can improve my coding skills, ability to write and also share what I'm learning. A place I can always refer to for my growth and in doing so share my knowledge and failings.&lt;/p&gt;

&lt;p&gt;Here's a bit about me:&lt;br&gt;
 My name is &lt;strong&gt;Jonathan Roy Mugisha&lt;/strong&gt; and I'm a software developer and I'm currently learning JavaScript and building projects. Best way to master any skill. When I'm not coding, I read books, play video games and do sports.&lt;/p&gt;

&lt;p&gt;If all goes to plan, I'll be sharing what I learn on here on my journey to becoming Full-Stack. It's never too early for feedback. Thanks for reading and see you in the next one!✨&lt;/p&gt;

</description>
      <category>firstpost</category>
      <category>frontend</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
