{"id":206,"date":"2010-11-03T17:25:46","date_gmt":"2010-11-03T16:25:46","guid":{"rendered":"https:\/\/d-mueller.de\/blog\/?p=206"},"modified":"2016-01-12T00:08:04","modified_gmt":"2016-01-11T23:08:04","slug":"what-you-need-to-know-about-the-dom-20-functions-in-100-lines","status":"publish","type":"post","link":"https:\/\/d-mueller.de\/blog\/what-you-need-to-know-about-the-dom-20-functions-in-100-lines\/","title":{"rendered":"What you need to know about the DOM: 20 functions in 100 lines"},"content":{"rendered":"<p>Okay, first english article. I&#8217;m feeling kind of stupid when posting code related topics in German so I&#8217;m gonna try to bring some english content in here. Personally I think that comments, variable names and everything code-related should be written in english &#8211; considering multi-national development teams.<\/p>\n<p>So, enough introduction. What follows is one single function performing stupid javascript actions with the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Document_Object_Model\">Document Object Model<\/a>. Even though every sane developer prefers the use of a library like jQuery for stuff like that, it&#8217;s always good to know what goes on behind the scenes. What I&#8217;ve covered are the following functions:<\/p>\n<ul>\n<li><b>document.<\/b>\n<ul>\n<li>createElement<\/li>\n<li>createTextNode<\/li>\n<li>getElementsByTagName<\/li>\n<li>getElementsByName<\/li>\n<li>getElementById<\/li>\n<\/ul>\n<\/li>\n<li><b>node.<\/b>\n<ul>\n<li>appendChild<\/li>\n<li>removeChild<\/li>\n<li>className<\/li>\n<li>setAttribute<\/li>\n<li>getAttribute<\/li>\n<li>children<\/li>\n<li>tagName<\/li>\n<li>parentNode<\/li>\n<li>style.setProperty<\/li>\n<li>style.removeProperty<\/li>\n<li>style.cssText<\/li>\n<li>innerHTML<\/li>\n<li>insertBefore<\/li>\n<li>previousSibling<\/li>\n<li>nextSibling<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>I think the code is document good enough, feel free to play around with it.<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n    &lt;head&gt;\r\n    &lt;script&gt;\r\n    function doStuff()\r\n\t{\r\n\t\tvar container = document.createElement(&quot;div&quot;),\r\n\t\t\th1 = document.createElement(&quot;h1&quot;),\r\n\t\t\ttext = document.createTextNode(&quot;important headline&quot;),\r\n\t\t\tbody = document.getElementsByTagName(&quot;body&quot;)[0];\r\n\t\t\r\n\t\t\/\/add the text &#039;important headline&#039; to the h1\r\n\t\th1.appendChild(text); \r\n\t\th1.className = &quot;headline&quot;; \r\n\t\t\/\/add &lt;h1&gt;important headline&lt;\/h1&gt; to the container\r\n\t\tcontainer.appendChild(h1); \r\n\t\tcontainer.setAttribute(&#039;id&#039;,&#039;wrapper&#039;);\r\n\t\t\/\/add the container with h1 and \r\n\t\t\/\/text to the body (make it visible)\r\n\t\tbody.appendChild(container); \r\n\t\t\r\n\t\t\/*\r\n\t\tMarkup is now:\r\n\t\t&lt;body&gt;\r\n\t\t\t&lt;div id=&quot;wrapper&quot;&gt;\r\n\t\t\t\t&lt;h1 class=&quot;headline&quot;&gt;wichtige \u00dcberschrift&lt;\/h1&gt;\r\n\t\t\t&lt;\/div&gt;\r\n\t\t&lt;\/body&gt;\r\n\t\t*\/\r\n\r\n\t\t\/\/DIV == DIV -&gt; true\r\n\t\t\/\/tagName is basically the same as nodeName, \r\n\t\t\/\/except for the behaviour with text nodes\r\n\t\talert(body.children[0].tagName == h1.parentNode.tagName); \r\n\t\t\t\r\n\t\t\/\/uh oh, we &quot;accidently&quot; deleted the container \r\n\t\t\/\/variable (not the container itself)\r\n\t\tcontainer = null; \r\n\t\t\r\n\t\t\/\/so we get it again\r\n\t\tcontainer = document.getElementById(&quot;wrapper&quot;); \r\n\t\t\r\n\t\t\/\/some css stuff\r\n\t\tcontainer.style.setProperty(&quot;border&quot;,&quot;4px solid red&quot;);\r\n\t\t\/\/overwrites all previous styles like the border set before\r\n\t\tvar cssText = &#039;color:blue;text-decoration:underline;&#039;;\r\n\t\tcontainer.style.cssText = cssText; \r\n\t\t\/\/other way to do that: container.setAttribute(&#039;style&#039;,cssText);\r\n\t\t\r\n\t\t\/\/make it black again\r\n\t\tcontainer.style.removeProperty(&quot;color&quot;); \r\n\t\t\r\n\t\t\/\/set name\r\n\t\th1.setAttribute(&quot;name&quot;,&quot;theName&quot;); \r\n\t\talert(h1.getAttribute(&quot;name&quot;)); \r\n\t\t\/\/and remove its class\r\n\t\th1.removeAttribute(&#039;class&#039;); \r\n\t\th1.innerHTML = &quot;my new headlinetext&quot;;\r\n\t\t\r\n\t\t\/\/ah damn, same again ;)\r\n\t\th1 = null;\r\n\t\t\r\n\t\t\/\/recover h1\r\n\t\th1 = document.getElementsByName(&quot;theName&quot;)[0];\r\n\t\t\r\n\t\t\/*\r\n\t\tmarkup is now:\r\n\t\t&lt;body&gt;\r\n\t\t\t&lt;div id=&quot;wrapper&quot; style=&quot;text-decoration: underline;&quot;&gt;\r\n\t\t\t\t&lt;h1 name=&quot;theName&quot;&gt;my new headlinetext&lt;\/h1&gt;\r\n\t\t\t&lt;\/div&gt;\r\n\t\t&lt;\/body&gt;\r\n\t\t*\/\r\n\t\t\r\n\t\tvar span = document.createElement(&quot;span&quot;);\r\n\t\tspan.appendChild(document.createTextNode(&quot;here comes the span&quot;));\r\n\t\tcontainer.insertBefore(span,h1);\r\n\t\t\r\n\t\t\/*\r\n\t\tmarkup is now:\r\n\t\t&lt;body&gt;\r\n\t\t\t&lt;div id=&quot;wrapper&quot; style=&quot;text-decoration: underline; &quot;&gt;\r\n\t\t\t\t&lt;span&gt;here comes the span&lt;\/span&gt;\r\n\t\t\t\t&lt;h1 align=&quot;center&quot;&gt;my new headlinetext&lt;\/h1&gt;\r\n\t\t\t&lt;\/div&gt;\r\n\t\t&lt;\/body&gt;\r\n\t\t*\/\r\n\t\t\r\n\t\talert(h1.previousSibling.tagName == span.tagName); \/\/span == span\r\n\t\talert(span.nextSibling.tagName == h1.tagName); \/\/h1 == h1\r\n\t\t\r\n\t\tcontainer.removeChild(h1); \/\/and the headline is gone\r\n\t\t\r\n\t\t\/*\r\n\t\tmarkup is now:\r\n\t\t&lt;body&gt;\r\n\t\t\t&lt;div id=&quot;wrapper&quot; style=&quot;text-decoration: underline; &quot;&gt;\r\n\t\t\t\t&lt;span&gt;here comes the span&lt;\/span&gt;\r\n\t\t\t&lt;\/div&gt;\r\n\t\t&lt;\/body&gt;\r\n\t\t*\/\r\n\t}\r\n    \r\n    window.onload = doStuff;\r\n    &lt;\/script&gt;\r\n    &lt;\/head&gt;\r\n    &lt;body&gt;\r\n    &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Okay, first english article. I&#8217;m feeling kind of stupid when posting code related topics in German so I&#8217;m gonna try to bring some english content in here. Personally I think that comments, variable names and everything code-related should be written &hellip; <a href=\"https:\/\/d-mueller.de\/blog\/what-you-need-to-know-about-the-dom-20-functions-in-100-lines\/\">Weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,6,3],"tags":[],"class_list":["post-206","post","type-post","status-publish","format-standard","hentry","category-javascript","category-security","category-webdev"],"_links":{"self":[{"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/posts\/206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/comments?post=206"}],"version-history":[{"count":0,"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/posts\/206\/revisions"}],"wp:attachment":[{"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/media?parent=206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/categories?post=206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/tags?post=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}