{"id":4581,"date":"2023-02-21T10:33:27","date_gmt":"2023-02-21T10:33:27","guid":{"rendered":"https:\/\/isophal.com\/?p=4581"},"modified":"2023-02-21T10:33:28","modified_gmt":"2023-02-21T10:33:28","slug":"print-the-content-of-a-div-element-using-javascript","status":"publish","type":"post","link":"https:\/\/isophal.com\/news\/2023\/02\/21\/4581.html\/","title":{"rendered":"Print the content of a div element using JavaScript"},"content":{"rendered":"\n<p><strong>Example 1:<\/strong>\u00a0This example uses JavaScript window print command to print the content of\u00a0<a href=\"https:\/\/www.geeksforgeeks.org\/div-tag-html\/\">div element<\/a>.\u00a0<\/p>\n\n\n\n<p><strong>Sample Solution:<\/strong><\/p>\n\n\n\n<p><br><strong>Print the content of a div<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-- Script to print the content of a div --&gt;\n&lt;script&gt;\n    function printDiv() {\n        var divContents = document.getElementById(\"GFG\").innerHTML;\n        var a = window.open('', '', 'height=500, width=500');\n        a.document.write('&lt;html&gt;');\n        a.document.write('&lt;body &gt; &lt;h1&gt;Div contents are &lt;br&gt;');\n        a.document.write(divContents);\n        a.document.write('&lt;\/body&gt;&lt;\/html&gt;');\n        a.document.close();\n        a.print();\n    }\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div id=\"GFG\" style=\"background-color: green;\"&gt;\n\n    &lt;h2&gt;Geeksforgeeks&lt;\/h2&gt;\n\n    &lt;p&gt;\n        This is inside the div and will be printed\n        on the screen after the click.\n    &lt;\/p&gt;\n&lt;\/div&gt;\n\n&lt;input type=\"button\" value=\"click\" onclick=\"printDiv()\"&gt;<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/media.geeksforgeeks.org\/wp-content\/uploads\/20221114115209\/divprint1.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>How to create a pop-up to print dialog box using JavaScript?<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html>\r\n&lt;html>\r\n\r\n&lt;head>\r\n\t&lt;title>create a pop-up to print\r\n\tdialog box using JavaScript&lt;\/title>\r\n&lt;\/head>\r\n\r\n&lt;body>\r\n\t&lt;center>\r\n\t\t&lt;h1 style=\"color:rgb(255, 174, 0)\">Print dialog box using JavaScript&lt;\/h1>\r\n\t\t&lt;script>\r\n\t\t\tfunction printPopUp() {\r\n\t\t\t\talert(\"Pop-up dialog-box\")\r\n\t\t\t\twindow.print();\r\n\t\t\t}\r\n\t\t&lt;\/script>\r\n\r\n\t\t&lt;button onclick=\"printPopUp()\">Print&lt;\/button>\r\n\t&lt;\/center>\r\n&lt;\/body>\r\n\r\n&lt;\/html><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html>\r\n&lt;html>\r\n\r\n&lt;head>\r\n\t&lt;title>create a pop-up to print\r\n\tdialog box using JavaScript&lt;\/title>\r\n&lt;\/head>\r\n\r\n&lt;body>\r\n\t&lt;center>\r\n\t\t&lt;h1 style=\"color:rgba(255, 136, 0, 0.918)\">\r\n            Print dialog box using JavaScript\r\n\t&lt;\/h1>\r\n\t\t&lt;a href=\"javascript:alert('Pop-up dialog-box');window.print();\">\r\n\t\tClick Me\r\n\t&lt;\/a>\r\n&lt;\/center>\r\n&lt;\/body>\r\n\r\n&lt;\/html><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Example 1:\u00a0This example uses JavaScript window print&hellip;<\/p>\n","protected":false},"author":1,"featured_media":4582,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[423],"tags":[76],"class_list":["post-4581","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-javascript"],"_links":{"self":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/4581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/comments?post=4581"}],"version-history":[{"count":1,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/4581\/revisions"}],"predecessor-version":[{"id":4583,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/4581\/revisions\/4583"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/media\/4582"}],"wp:attachment":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/media?parent=4581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/categories?post=4581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/tags?post=4581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}