{"id":2585,"date":"2021-08-04T18:42:40","date_gmt":"2021-08-04T13:12:40","guid":{"rendered":"http:\/\/uitutorials.in\/wp\/?p=2585"},"modified":"2021-08-10T18:29:34","modified_gmt":"2021-08-10T12:59:34","slug":"dom","status":"publish","type":"post","link":"https:\/\/uitutorials.in\/wp\/dom\/","title":{"rendered":"DOM"},"content":{"rendered":"<pre class=\"line-numbers\"><code class=\"language-javascript\">&lt;div id=\"page\"&gt;\r\n      &lt;h1 id=\"header\"&gt;TO DO LIST&lt;\/h1&gt;\r\n      &lt;ul&gt;\r\n        &lt;h2&gt;DataFlair\u2019s JavaScript Tutorial&lt;\/h2&gt;&lt;DataFlair\u2019s&gt;\r\n        &lt;li id=\"one\" class=\"mandatory\"&gt;Learning the concepts&lt;\/li&gt;\r\n        &lt;li id=\"two\" class=\"mandatory\"&gt;Practising the codes&lt;\/li&gt;\r\n        &lt;li id=\"three\"&gt;Taking quizzes&lt;\/li&gt;\r\n        &lt;li id=\"four\"&gt;Solving Interview Questions&lt;\/li&gt;\r\n      &lt;\/ul&gt;\r\n      &lt;!-- JavaScript code --&gt;\r\n&lt;\/div&gt;<\/code><\/pre>\n<pre>\r\n1. Creation of the DOM tree\r\n2. Manipulation of the DOM tree\r\n<\/pre>\n<p>Every element, attribute, and text content in the HTML creates its own DOM node in the tree. A DOM tree consists of four main types of nodes:<\/p>\n<p><strong>Document node:<\/strong><br \/>\nThis is added at the top of the tree and represents the entire page in the browser. As stated above, it is the starting point in the DOM tree; you need to navigate via the document node to access any other node in your DOM tree.<\/p>\n<p><strong>Element nodes:<\/strong><br \/>\nAll the HTML elements like heading tags (&lt;h1&gt; to &lt;h6&gt;) and paragraph tags (&lt;p&gt;) in the page create an element node in the tree. You use these nodes to gain access to the elements\u2019 attribute and text nodes.<\/p>\n<p><strong>Attribute nodes:<\/strong><br \/>\nWhen the opening tags in the HTML document contain attributes, the tree represents them as attribute nodes. These are not the children of the element nodes but a part of them.<\/p>\n<p><strong>Text nodes:<\/strong><br \/>\nOnce you have access to the element node, you can reach the text content within that element, stored inside the text nodes of the DOM tree. These nodes cannot have child nodes. Thus, a text node always creates a new branch in the DOM tree, and no further branches come out of it.<\/p>\n<h3>Methods to Select an INDIVIDUAL Element Node<\/h3>\n<p>Following are the methods to select an individual element in the tree:<\/p>\n<p><strong>getElementById(\u2018id\u2019):<\/strong> Uses the unique value of the element\u2019s id attribute. The HTML must have an id attribute for the method to select it.<br \/>\nFor example \u2013 getElementById(\u2018one\u2019)<\/p>\n<p><strong>querySelector(\u2018css selector\u2019):<\/strong> Uses a CSS selector, returns the first matching element.<br \/>\nFor example \u2013 querySelector(\u2018h1\u2019)<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">&lt;script type=\"text\/JavaScript\"&gt;\r\n                       document.getElementById('one').style.color=\"maroon\"; \/\/change font color\r\n        document.querySelector(\"h1\").style.backgroundColor = \"blue\"; \/\/change background color\r\n&lt;\/script&gt;<\/code><\/pre>\n<h3>Methods to Select Multiple Elements (NodeLists)<\/h3>\n<p>There are three common ways to select multiple elements in the tree:<\/p>\n<p><strong>getElementsByClassName(): <\/strong>Selects all the elements that have a specified value for the class attribute.<br \/>\nFor example \u2013 getElementsByClassName(\u2018mandatory\u2019)<\/p>\n<p><strong>getElementsByTagName():<\/strong> Selects all the elements that have the specified tag names.<br \/>\nFor example \u2013 getElementsByTagName(\u2018h1\u2019)<\/p>\n<p><strong>querySelectorAll():<\/strong> Uses a CSS selector, returns all the matching elements.<br \/>\nFor example \u2013 querySelectorAll(\u2018li.mandatory\u2019)<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">&lt;script type=\"text\/JavaScript\"&gt;\r\n        var list_qs = document.querySelectorAll(\"li.mandatory\"); \/\/NodeList\r\n        list_qs[0].style.backgroundColor = \"blue\"; \/\/change background color\r\n        var list_cn = document.getElementsByClassName('mandatory'); \/\/NodeList\r\n        list_cn[0].style.color=\"white\"; \/\/change font color\r\n        var list_tn = document.getElementsByTagName('h1'); \/\/NodeList\r\n        list_tn[0].style.color=\"gray\"; \/\/change font color\r\n&lt;\/script&gt;<\/code><\/pre>\n<h3>innerText, textcontent, innerHTML<\/h3>\n<p>Both these properties replace the entire content of the element, including all the markup present. But you should avoid using innerText since it is not part of any standard. Also, this method slows the speed of content loading on the page.<\/p>\n<p>The innerHTML property gets the content of the element and returns it as one long string, including the markup it contains. But you should be careful of where you use this property since there are some security risks associated with it.<\/p>\n<pre>\r\n* innerText was non-standard, textContent was standardized earlier.\r\n* innerText returns the visible text contained in a node, while textContent returns the full text. \r\n -For example, on the following HTML &lt;span&gt;Hello &lt;span style=\"display: none;\"&gt;World&lt;\/span&gt;&lt;\/span&gt;, innerText will return 'Hello', while textContent will return 'Hello World'.\r\n* As a result, innerText is much more performance-heavy: it requires layout information to return the result.\r\n* innerText is defined only for HTMLElement objects, while textContent is defined for all Node objects.\r\n* Another difference in behavior between innerText and textContent: If you change the text-transform of an element by CSS, it will affect the result of 'innerText', but not the result of textContent. For example: innerText of &lt;div style=\"text-transform: uppercase;\"&gt;Hello World&lt;\/div&gt; will be \"HELLO WORLD\", while textContent will be \"Hello World\"\r\n<\/pre>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">&lt;div id = \"page\"&gt;\r\n      &lt;h3&gt;Accessing and Updating Element Content&lt;\/h3&gt;\r\n      &lt;p id = \"p\"&gt;&lt;em&gt;DataFlair&lt;\/em&gt; tutorial&lt;\/p&gt;\r\n      &lt;p&gt;&lt;em id = \"p1\"&gt;&lt;\/em&gt;&lt;\/p&gt;\r\n      &lt;p&gt;&lt;em id = \"p2\"&gt;&lt;\/em&gt;&lt;\/p&gt;\r\n      &lt;p&gt;&lt;em id = \"p3\"&gt;&lt;\/em&gt;&lt;\/p&gt;\r\n      &lt;!-- JavaScript Code --&gt;\r\n      &lt;script&gt;\r\n        document.write(\"&lt;b&gt;Accessing elements:&lt;\/b&gt;&lt;\/br&gt;\");\r\n        var x = document.getElementById('p').textContent;\r\n        document.write(\"textContent: \" + x + \"&lt;\/br&gt;\");\r\n        var y = document.getElementById('p').innerText;\r\n        document.write(\"innerText: \" + y + \"&lt;\/br&gt;\");\r\n        var z = document.getElementById('p').innerHTML;\r\n        document.write(\"innerHTML: \" + z + \"&lt;\/br&gt;\");\r\n        \/\/updating elements\r\n        document.getElementById('p1').textContent = \"&lt;strong&gt;JavaScript&lt;\/strong&gt; tutorials\";\r\n        document.getElementById('p2').innerText = \"&lt;strong&gt;JavaScript&lt;\/strong&gt; quizzes\";\r\n        document.getElementById('p3').innerHTML = \"&lt;strong&gt;JavaScript&lt;\/strong&gt; interview questions\";\r\n      &lt;\/script&gt;\r\n    &lt;\/div&gt;<\/code><\/pre>\n<h3>add\/remove HTML content is DOM Manipulation.<\/h3>\n<p>Another technique to <strong>add\/remove<\/strong> HTML content is DOM Manipulation. To add HTML content to your webpage, you need to follow the three steps discussed below:<\/p>\n<p>Create a new element using createElement().<br \/>\nGive it content by creating a text node using createTextNode() and adding it to the element using the appendChild() method.<br \/>\nAdd the element to the DOM tree by finding the location and then using appendChild().<br \/>\nTo remove an element from the webpage, the steps are as follows:<\/p>\n<p>Store the element you want to remove in a variable.<br \/>\nStore the parent of that element in another variable.<br \/>\nRemove the element from its containing element using the removeChild() method.<br \/>\nLet\u2019s implement these methods with the help of a code. We will use the same HTML and CSS code we discussed at the beginning of this tutorial. All you need to do is change the JavaScript in your program. In this example, we will add a paragraph &lt;p&gt; tag in the page, and remove the &lt;h1&gt; tag from it.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">&lt;script type=\"text\/JavaScript\"&gt;\r\n        \/\/Adding new element\r\n        var newEl = document.createElement('p'); \/\/create element node\r\n        var newText = document.createTextNode('Node added using DOM manipulation.'); \/\/create text node\r\n        newEl.appendChild(newText); \/\/add text node to element node\r\n        var position = document.getElementById(\"page\"); \/\/find the position where you want to add the node\r\n        position.appendChild(newEl); \/\/add element in that position\r\n        \/\/Removing an element\r\n        var removeEl = document.getElementById('header'); \/\/store the element you want to remove\r\n        var containerEl = document.getElementById('page'); \/\/find the element which contains the above element\r\n        containerEl.removeChild(removeEl); \/\/remove the element\r\n&lt;\/script&gt;<\/code><\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/javascript-dom\/\" rel=\"noopener\" target=\"_blank\">Data-Flair<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt;div id=&#8221;page&#8221;&gt; &lt;h1 id=&#8221;header&#8221;&gt;TO DO LIST&lt;\/h1&gt; &lt;ul&gt; &lt;h2&gt;DataFlair\u2019s JavaScript Tutorial&lt;\/h2&gt;&lt;DataFlair\u2019s&gt; &lt;li id=&#8221;one&#8221; class=&#8221;mandatory&#8221;&gt;Learning the concepts&lt;\/li&gt; &lt;li id=&#8221;two&#8221; class=&#8221;mandatory&#8221;&gt;Practising the codes&lt;\/li&gt; &lt;li id=&#8221;three&#8221;&gt;Taking quizzes&lt;\/li&gt; &lt;li id=&#8221;four&#8221;&gt;Solving Interview Questions&lt;\/li&gt; &lt;\/ul&gt; &lt;!&#8211; JavaScript code &#8211;&gt; &lt;\/div&gt; 1. Creation of the DOM tree 2. Manipulation of the DOM tree Every element, attribute, and text<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[153],"class_list":["post-2585","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-dom","ct-col-2"],"_links":{"self":[{"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/posts\/2585","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/comments?post=2585"}],"version-history":[{"count":4,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/posts\/2585\/revisions"}],"predecessor-version":[{"id":2608,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/posts\/2585\/revisions\/2608"}],"wp:attachment":[{"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/media?parent=2585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/categories?post=2585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/tags?post=2585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}