<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rahul gandhi on Medium]]></title>
        <description><![CDATA[Stories by Rahul gandhi on Medium]]></description>
        <link>https://medium.com/@rahulgandhi38?source=rss-951d0199cde1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*fmOOikKNvkBnewn5BxNTQQ.jpeg</url>
            <title>Stories by Rahul gandhi on Medium</title>
            <link>https://medium.com/@rahulgandhi38?source=rss-951d0199cde1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 09:14:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rahulgandhi38/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[DOM Manipulation in Javascript]]></title>
            <link>https://medium.com/@rahulgandhi38/dom-manipulation-in-javascript-198fbc30737b?source=rss-951d0199cde1------2</link>
            <guid isPermaLink="false">https://medium.com/p/198fbc30737b</guid>
            <category><![CDATA[html]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[jquery]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web]]></category>
            <dc:creator><![CDATA[Rahul gandhi]]></dc:creator>
            <pubDate>Thu, 08 Mar 2018 03:19:07 GMT</pubDate>
            <atom:updated>2018-03-12T18:31:06.264Z</atom:updated>
            <content:encoded><![CDATA[<p>When it comes to DOM manipulation, the first thing that pops into our mind is jQuery. But it’s 2018 and we don’t use jQuery anymore.</p><p>And thanks to the great <a href="https://developer.mozilla.org/en-US/docs/Web/API">Web API</a> which provides us with all of the tools we need. So let’s deep dive into the DOM manipulation using Javascript.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5trT5aYf1eoO80CgArJSGg.png" /><figcaption>❤ <strong>Javascfript</strong> ❤</figcaption></figure><h3>What is a DOM?</h3><p>DOM (Document Object Model) is a programming API for the documents (web pages) which can be used by programs ( like Javascript ) to change the document structure, style, and content. The DOM represents the document as nodes and objects.</p><p>The name “Document Object Model” was chosen because it is an “<a href="https://www.w3.org/TR/DOM-Level-2-Core/glossary.html#dt-object-model"><em>object model</em></a>” in the traditional object oriented design sense.</p><p>Few points to consider:</p><blockquote>Is the HTML we write is the DOM? No</blockquote><blockquote>Does the Page Source show the DOM? No</blockquote><blockquote>Does the (Inspect element) DevTools show the DOM? <strong>Yes, a visual representation of the DOM.</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b-xK-5f8rneQHRCwAI7mEw.png" /><figcaption>A visual representation of the DOM</figcaption></figure><h3>Creation:</h3><p>To create an HTML element we use <strong>document.createElement</strong>()</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RmdShFSgQ2JgGO9WQxfRAQ.png" /><figcaption>New element is returned</figcaption></figure><p><strong>createElement</strong>() method converts the tagName (‘div’ in our case) to lowerCase before creating the element.</p><p>If no tag name is provided, then createElement() will throw an Uncaught TypeError. It requires at least 1 argument to create an element.</p><h3>Insertion:</h3><p><strong>Node.appendChild</strong>() method appends the element as the last child of the specified node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xANdVHX3DaBKJzPhTXzCkg.png" /><figcaption>The returned value is the appended child</figcaption></figure><p><strong>Node.insertBefore() </strong>method inserts a node before the reference node as a child of a specified parent node. The syntax will make it clear.</p><pre>let <em>insertedNode</em> = <em>parentNode</em>.insertBefore(<em>newNode</em>, <em>referenceNode</em>);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cDF_7gOv79hAOhaHN8CzGQ.png" /><figcaption>The returned value is the appended child</figcaption></figure><h3>Removal:</h3><p><strong>Node.removeChild() </strong>method removes the child node from the DOM. Let’s remove the node we added in the above example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/832/1*cSdMh6Y7LbvmFe2rWfqj_A.png" /><figcaption>The returned value is the removed node</figcaption></figure><h3>Style:</h3><p><strong>Setting Style:</strong></p><p>We can set multiple styles at once using the <a href="https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration">CSSStyleDeclaration</a> object (element.style.cssText)<em> </em>which will overwrite the previous inline style.<br>It avoids repainting the element every time you change a property (you change them all “at once” somehow).</p><p>To keep previous inline styles intact, we can use <em>element.style </em>property and target a specific style.</p><pre>1. Multiple styles in a single statement</pre><pre>element.style.cssText = &quot;color: blue; border: 1px solid black&quot;; <br>or<br>element.setAttribute(&quot;style&quot;, &quot;color:red; border: 1px solid blue;&quot;);</pre><pre>2. Setting specific style while leaving other inline style values untouched</pre><pre>element.style.color = &quot;blue&quot;;</pre><p>A style declaration is reset by setting it to null</p><pre>element.style.color = null</pre><p><strong>Getting Style:</strong></p><p><strong>element.style </strong>returns the element’s inline style attribute.</p><p><strong>window.getComputedStyle() </strong>method returns the values of all CSS properties of an element after applying active stylesheets.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TY2MumjoYZ_1f-fW3p-noQ.png" /><figcaption>CSSStyleDeclaration object</figcaption></figure><p>The returned style is a <em>live</em> CSSStyleDeclaration object, which updates itself automatically when the element&#39;s style is changed</p><h3>Classes:</h3><p><strong>className </strong>can be used to get and set the classes on an element.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uOOQRnqaIj26F2J1u4bAcA.png" /><figcaption>className returns <em>space-separated classes of the current element</em></figcaption></figure><p><strong>classList </strong>is an amazing alternative to accessing the element’s classes. It returns the class name(s) of an element, as a DOMTokenList object and provides several methods like add, remove, toggle etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zenJe1h3JwSisJFpW81tTQ.png" /></figure><h3>ID:</h3><p>Element.id property represents the global id attribute. It must be unique in a document. It can be used to get or set id of an element.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EfKBbqhrdE-sKKxhwwySTw.png" /><figcaption>This attribute’s value must not contain whitespace</figcaption></figure><h3>Locating DOM Elements:</h3><p>There are several ways to locate the elements in the DOM</p><ul><li><em>document.getElementById</em>(): returns the element that has the ID attribute with the specified value. Returns null if no element is found. <em>It is only available as a method of the global document object</em>. (ID are unique across the document so no need for a local version).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*33JR9UnTOcQoJwVL9Pgrog.png" /><figcaption><em>The Error occurred because document.getElementById is only available as a method on the document, not on all of the elements.</em></figcaption></figure><blockquote>Except document.getElementById(), all of the below mentioned methods are available as a method on all element objects in the DOM.</blockquote><ul><li><em>document.getElementsByClassName</em>(): returns an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice#Array-like_objects">array-like object</a> (*live <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection">HTMLCollection</a>) of all the <strong>child</strong> elements who have all of the given class names.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FmHBpp_Uf1BKtnXwCuEvHQ.png" /><figcaption><em>document.getElementsByClassName</em>() is available on all of the elements.</figcaption></figure><blockquote>The <strong>live</strong> HTMLCollection means it updates itself automatically to stay in sync with the DOM tree without having to call the method again.</blockquote><ul><li><em>document.getElementsByTagName</em>(): returns an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice#Array-like_objects">array-like object</a>(*live <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection">HTMLCollection</a>) of all the elements in the document with the specified tag name. The method starts searching at the specified element and searches all the descendant elements for the given Tag name.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nzFmzwI0l9Bc-kU5DG7XiQ.png" /><figcaption>HTML Tags, everywhere :)</figcaption></figure><ul><li><em>document.querySelector</em>(): returns the first element which matches the specified selector (selector can be id, class, tag, any other valid <a href="https://www.w3.org/TR/selectors-3/">CSS selector</a> or their combination)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DsM9QCpvGtIr7B7LsSKWPQ.png" /><figcaption>Any valid CSS selector can be used</figcaption></figure><ul><li><em>document.querySelectorAll</em>(): returns a list (a static NodeList) of the elements that match the specified group of selectors.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9R7HpF61NCRIu3P_9byOLw.png" /><figcaption>NodeList is also an array-like object!</figcaption></figure><h3>Attributes:</h3><p>Attributes are key/value pair of strings that represent any information about the element. To access them, we have</p><ul><li><em>element.attributes</em>: returns a NamedNodeMap of all the attributes node registered to the specified node.</li><li><em>element.get</em>Attribute<em>()</em>: returns the value of a specified attribute on the element.</li><li><em>element.set</em>Attribute<em>()</em>: sets the value of specified attribute on the element. If the attribute already exists, the value is updated; otherwise, a new attribute is added with the specified name and value.</li><li><em>element.has</em>Attribute<em>():</em> returns a Boolean value indicating whether the element has the specified attribute or not.</li><li><em>element.</em>removeAttribute<em>()</em>: removes the specified attribute on the element.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DjhU-zews7aCOVQ_ti7Gww.png" /><figcaption>Attributes are the lifeline of the elements :)</figcaption></figure><h3>Modification:</h3><ul><li><em>Element.innerHTML</em> gets or sets the HTML content of an element’s descendants.</li></ul><p>The entire contents of the document body can be deleted by:</p><pre>document.body.innerHTML = &quot;&quot;;  <br>// Replaces body content with an empty string.</pre><ul><li>Node.textContent: gets or sets the text content of a node and its descendants. <em>It uses straight text, does not parse HTML, and is faster.</em></li><li>Node.innerText: gets or sets the “rendered” text content of a node and its descendants.</li></ul><blockquote>innerText<em> is aware of style and will not return the text of hidden elements, whereas </em>textContent<em> will.</em></blockquote><h3>Children:</h3><p>Let’s look into few read-only properties to access the children.</p><blockquote>All of the below mentioned properties would return only child <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element">element</a> of the node.</blockquote><ul><li>Node.children: returns a live HTMLCollection of the child <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element">elements</a>.</li><li>Node.firstElementChild: returns the first child of the node.</li><li>Node.lastElementChild: returns the last child of the node.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nxQ3v5cWmlBPJHuji5qCCw.png" /><figcaption>If the node has no element children, then children is an empty list with a length of 0.</figcaption></figure><blockquote>All of the below mentioned properties would return ‘all child <a href="https://developer.mozilla.org/en-US/docs/Web/API/Node">nodes</a>’ including non-element nodes like text-nodes and comment-nodes.</blockquote><ul><li>Node.childNodes: returns a live collection of the child <a href="https://developer.mozilla.org/en-US/docs/Web/API/Node">nodes</a>.</li><li>Node.firstChild: returns the first child of the node.</li><li>Node.lastChild: returns the last child of the node.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xYbhQ3BOD6orQNX7JNK1GQ.png" /><figcaption>childNodes vs children</figcaption></figure><h3>Siblings:</h3><p>Same way, we have a few read-only properties to access siblings:</p><ul><li>nextSibling: returns the node immediately following the specified node.(right-sided sibling)</li><li>previousSibling: returns the node immediately before the specified node. (left-sided sibling)</li></ul><p>For accessing only the elements, we use <strong><em>nextElementSibling</em> </strong>and<strong> <em>previousElementSibling</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZmBX04Nyma4fyEe-hrxgoA.png" /><figcaption>previousSibling and nextSibling contains text-nodes in this case</figcaption></figure><p>Thanks for reading :)</p><p>Let’s work together and make the web a better place.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=198fbc30737b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>