{"id":1486,"date":"2019-04-22T12:54:58","date_gmt":"2019-04-22T07:24:58","guid":{"rendered":"http:\/\/blog.openwebsolutions.in\/?p=1486"},"modified":"2019-04-22T12:54:58","modified_gmt":"2019-04-22T07:24:58","slug":"simplified-methods-of-working-with-dom","status":"publish","type":"post","link":"https:\/\/openwebsolutions.in\/blog\/simplified-methods-of-working-with-dom\/","title":{"rendered":"Simplified methods of working with DOM"},"content":{"rendered":"<p class=\"p1\"><span class=\"s1\">A web page is a document which can be displayed in the web browsers or as the HTML source but ultimately both are the same document. Document Object Model (DOM) represents the same document to be manipulated. An object-oriented representation of any web page is called DOM. This web page can be modified with JavaScript.<\/span><\/p>\n<p class=\"p1\"><span class=\"s1\">Document Object Model (DOM) is also an interface for HTML and XML documents for programming. DOM represents the page in a way where programs can change the document structure, style, and content. DOM also represents the document as nodes and objects which allow programming languages to connect to the pages.<\/span><\/p>\n<p class=\"p2\"><span class=\"s2\">Most modern browsers extend\u00a0<span class=\"s3\">W3C DOM<\/span>\u00a0and\u00a0<span class=\"s3\">WHATWG DOM<\/span><\/span> <span class=\"s2\">standard for implementation. So, care must be taken at the time of using them on the web where documents may be accessed by various browsers with different DOMs.<\/span><\/p>\n<p class=\"p2\"><span class=\"s2\">In the code below, standard DOM specifies \u201c<\/span><span class=\"s5\">getElementsByTagName\u201d<\/span><span class=\"s2\">\u00a0method must return a list of all the\u00a0<\/span><span class=\"s5\">&lt;P&gt;<\/span><span class=\"s2\">\u00a0elements in the document:<\/span><\/p>\n<p class=\"p3\"><span class=\"s6\"><b>var paragraphs = document.getElementsByTagName(&#8220;P&#8221;);<\/b><\/span><\/p>\n<p class=\"p2\"><span class=\"s2\">All properties, methods, and events available for manipulating and creating web pages can be organized into objects. For example, the\u00a0<\/span><span class=\"s5\"><b>document<\/b><\/span><span class=\"s2\">\u00a0object that represents the document itself, the\u00a0<\/span><span class=\"s5\"><b>table<\/b><\/span><span class=\"s2\">\u00a0object that implements the special\u00a0<\/span><span class=\"s5\"><b>HTMLTableElement<\/b><\/span><span class=\"s2\">\u00a0DOM interface for accessing HTML tables and so on.<\/span><\/p>\n<p class=\"p1\"><span class=\"s1\">The short example above is written<i>\u00a0<\/i>in JavaScript which uses\u00a0the DOM to access the document and its elements. JavaScript language wouldn&#8217;t have any model or HTML\u00a0documents,\u00a0XML documents\u00a0or their component parts (e.g. elements) without DOM, though DOM is not a programming language.<\/span><\/p>\n<p class=\"p1\"><span class=\"s1\">Every element in a document (the head, tables within the document, table headers, text within the table cells) is part of DOM. They can all be accessed and manipulated using a scripting language like JavaScript for the purpose of DOM.<\/span><\/p>\n<p class=\"p1\"><span class=\"s1\">JavaScript and the DOM were tightly intertwined at the starting, but they evolved into separate entities afterward. The page content is stored in the DOM which may be accessed and manipulated via JavaScript, which may lead us to the following equation:<\/span><\/p>\n<p class=\"p1\"><span class=\"s1\"><b>API (HTML\u00a0or XML page) = DOM + JS (scripting language)<\/b><\/span><\/p>\n<p class=\"p1\"><span class=\"s1\">DOM was designed to be totally independent of any particular programming language. Programming languages which make the structural representation of the document available from a single, consistent API. Though our focus is exclusively on JavaScript, implementations of the DOM can be built for any language.<\/span><\/p>\n<p class=\"p4\"><span class=\"s1\"><b>Accessing the DOM<\/b><\/span><\/p>\n<p class=\"p1\"><span class=\"s1\">We don&#8217;t have to do anything special, to begin with, the DOM because every web browser uses some document object model to make web pages accessible via JavaScript.<\/span><\/p>\n<p class=\"p2\"><span class=\"s2\">When you create a script \u2013 whether it is inline in a\u00a0<\/span><span class=\"s1\"><b>&lt;script&gt;<\/b><\/span><span class=\"s2\">\u00a0element or included in the web page by means of a script loading instruction\u2013you can immediately begin using the API for the\u00a0<span class=\"s3\"><b>document<\/b><\/span>\u00a0or\u00a0<span class=\"s3\"><b>window<\/b><\/span>\u00a0elements to manipulate the document itself or to get the various elements in the web page.<\/span><\/p>\n<p class=\"p1\"><span class=\"s1\">DOM programming may be as simple as the following, which displays an alert message by using the\u00a0<span class=\"s3\"><b>alert()<\/b><\/span>\u00a0function from the\u00a0<span class=\"s3\"><b>window<\/b><\/span><\/span><span class=\"s3\"><b>\u00a0<\/b><\/span><span class=\"s1\">object. <\/span><\/p>\n<p class=\"p5\"><span class=\"s1\"><b>&lt;body onload=&#8221;window.alert(&#8216;Welcome to my home page!&#8217;);&#8221;&gt;<\/b><\/span><\/p>\n<p class=\"p1\"><span class=\"s1\">Or it will be more sophisticated DOM methods to actually create new content, as in the example below.<\/span><\/p>\n<p class=\"p6\"><span class=\"s6\"><b>&lt;html&gt;<\/b><\/span><\/p>\n<p class=\"p6\"><span class=\"s6\"><b>&lt;head&gt;<\/b><\/span><\/p>\n<p class=\"p6\"><span class=\"s6\"><b> &lt;script&gt;\u00a0<\/b><\/span><\/p>\n<p class=\"p6\"><span class=\"s6\"><b> window.onload = function() <\/b><\/span><\/p>\n<p class=\"p6\"><span class=\"s6\"><b>{<\/b><\/span><\/p>\n<p class=\"p6\"><span class=\"s6\"><b>var heading = document.createElement(&#8220;h1&#8221;);<\/b><\/span><\/p>\n<p class=\"p6\"><span class=\"s6\"><b>var heading_text = document.createTextNode(&#8220;Big Head!&#8221;);<\/b><\/span><\/p>\n<p class=\"p6\"><span class=\"s6\"><b>heading.appendChild(heading_text);<\/b><\/span><\/p>\n<p class=\"p6\"><span class=\"s6\"><b>document.body.appendChild(heading);<\/b><\/span><\/p>\n<p class=\"p6\"><span class=\"s6\"><b>}<\/b><\/span><\/p>\n<p class=\"p6\"><span class=\"s6\"><b>&lt;\/script&gt;<\/b><\/span><\/p>\n<p class=\"p6\"><span class=\"s6\"><b>&lt;\/head&gt;<\/b><\/span><\/p>\n<p class=\"p6\"><span class=\"s6\"><b>&lt;body&gt;<\/b><\/span><\/p>\n<p class=\"p6\"><span class=\"s6\"><b>&lt;\/body&gt;<\/b><\/span><\/p>\n<p class=\"p6\"><span class=\"s6\"><b>&lt;\/html&gt;<\/b><\/span><\/p>\n<p class=\"p8\"><span class=\"s1\">The common APIs in web and XML page scripting using the DOM are listed below:<\/span><\/p>\n<ol class=\"ol1\">\n<li class=\"li6\"><b><\/b><span class=\"s6\"><b>document.getElementById(id)<\/b><\/span><\/li>\n<li class=\"li6\"><b><\/b><span class=\"s6\"><b>document.getElementsByTagName(name)<\/b><\/span><\/li>\n<li class=\"li6\"><b><\/b><span class=\"s6\"><b>document.createElement(name)<\/b><\/span><\/li>\n<li class=\"li6\"><b><\/b><span class=\"s6\"><b>parentNode.appendChild(node)<\/b><\/span><\/li>\n<li class=\"li6\"><b><\/b><span class=\"s6\"><b>element.innerHTML<\/b><\/span><\/li>\n<li class=\"li6\"><b><\/b><span class=\"s6\"><b>element.setAttribute()<\/b><\/span><\/li>\n<li class=\"li6\"><b><\/b><span class=\"s6\"><b>element.getAttribute()<\/b><\/span><\/li>\n<li class=\"li6\"><b><\/b><span class=\"s6\"><b>element.addEventListener()<\/b><\/span><\/li>\n<li class=\"li6\"><b><\/b><span class=\"s6\"><b>window.content<\/b><\/span><\/li>\n<li class=\"li6\"><b><\/b><span class=\"s6\"><b>window.onload<\/b><\/span><\/li>\n<li class=\"li6\"><b><\/b><span class=\"s6\"><b>console.log()<\/b><\/span><\/li>\n<li class=\"li6\"><span class=\"s6\"><b>window.scrollTo()<\/b><\/span><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>A web page is a document which can be displayed in the web browsers or as the HTML source but ultimately both are the same document. Document Object Model (DOM) represents the same document to be manipulated. An object-oriented representation of any web page is called DOM. This web page can be modified with JavaScript. [&hellip;]<\/p>\n","protected":false},"author":32,"featured_media":1629,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[120],"tags":[125,121],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Simplified methods of working with DOM<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/openwebsolutions.in\/blog\/simplified-methods-of-working-with-dom\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Simplified methods of working with DOM\" \/>\n<meta property=\"og:description\" content=\"A web page is a document which can be displayed in the web browsers or as the HTML source but ultimately both are the same document. Document Object Model (DOM) represents the same document to be manipulated. An object-oriented representation of any web page is called DOM. This web page can be modified with JavaScript. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/openwebsolutions.in\/blog\/simplified-methods-of-working-with-dom\/\" \/>\n<meta property=\"og:site_name\" content=\"Openweb Solutions Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-22T07:24:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/04\/pic_htmltree.gif\" \/>\n\t<meta property=\"og:image:width\" content=\"486\" \/>\n\t<meta property=\"og:image:height\" content=\"266\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/\",\"name\":\"Openweb Solutions Blog\",\"description\":\"Transforming ideas into reality\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/openwebsolutions.in\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/simplified-methods-of-working-with-dom\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/04\/pic_htmltree.gif\",\"width\":486,\"height\":266},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/simplified-methods-of-working-with-dom\/#webpage\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/simplified-methods-of-working-with-dom\/\",\"name\":\"Simplified methods of working with DOM\",\"isPartOf\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/simplified-methods-of-working-with-dom\/#primaryimage\"},\"datePublished\":\"2019-04-22T07:24:58+00:00\",\"dateModified\":\"2019-04-22T07:24:58+00:00\",\"author\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/094fa2d34b71e8b2c73c005d1bd4c074\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/openwebsolutions.in\/blog\/simplified-methods-of-working-with-dom\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/094fa2d34b71e8b2c73c005d1bd4c074\",\"name\":\"Soumik Hens\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5028fba9f350d268e8a8845b54985006?s=96&r=g\",\"caption\":\"Soumik Hens\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1486"}],"collection":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/users\/32"}],"replies":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/comments?post=1486"}],"version-history":[{"count":6,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1486\/revisions"}],"predecessor-version":[{"id":1564,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1486\/revisions\/1564"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media\/1629"}],"wp:attachment":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media?parent=1486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/categories?post=1486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/tags?post=1486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}