etutorialspoint
  • Home
  • PHP
  • MySQL
  • MongoDB
  • HTML
  • Javascript
  • Node.js
  • Express.js
  • Python
  • Jquery
  • R
  • Kotlin
  • DS
  • Blogs
  • Theory of Computation

How to print specific part of a web page in javascript

It is a comman feature to take print out of a web page contents or some particular section of a web page. Such features are generally used in admin card print out, examination forms, some important notifications, etc. In this article, we provide you a simple way to take print of a specific section contents or full page contents of an HTML web page using Javascript. Javascript is used to make most interactive and dynamic web pages. It is most widely used because it can be easily integrated with HTML and almost all popular programming languages.

Print section of page using javascript

For this, we have created an HTML page and divide the full page contents in three sections. Each section contains print button to print that section. The first print button prints the full page content.

<html>
    <head>
        <style>
            h1{ display: inline;}
            #container { width: 500px; margin: 0 auto; }
            img { float: right;}
            .section { border: 2px solid #0197CA;
             border-radius: 3px; margin: 10px 0; padding: 5px;}
        </style>
        <script type="text/javascript">
            function printSection(el){
                var getFullContent = document.body.innerHTML;
                var printsection = document.getElementById(el).innerHTML;
                document.body.innerHTML = printsection;
                window.print();
                document.body.innerHTML = getFullContent;
            }
        </script>
    </head>
    <body>
    <h1>Content</h1>
	<a onclick="printSection('container')">
	<img title="Print Full Page Content" src="images/printicon.png" alt="Printicon" />
	</a>
   <div id="container">
   <div class="section">
      For this we are using HTML and simple javascript code.<br /><br />
      <h1>First Section</h2>
      <a onclick="printSection('section1')">
	   <img title="Print First Section" src="images/printicon.png" alt="" />
	  </a>
      <div id="section1">
	  Lorem Ipsum is simply dummy text of the printing and typesetting industry.
	  Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
	  when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
	  It has survived not only five centuries, but also the leap into electronic typesetting,
          remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets
          containing Lorem Ipsum passages, and more recently with desktop publishing software 
          like Aldus PageMaker including versions of Lorem Ipsum.</div>
   </div>
   <br /><br />
   <div class="section">
      <h1>Second Section</h1>
      <a onclick="printSection('section2')">
	    <img title="Print Second Section" src="images/printicon.png" alt="" />
	  </a>
      <div id="section2">Contrary to popular belief, Lorem Ipsum is not simply random text.
	  It has roots in a piece of classical Latin literature from 45 BC, making it over 
	  2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in
	  Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem
	  Ipsum passage, and going through the cites of the word in classical literature, 
	  discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of
	  "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC.
	  This book is a treatise on the theory of ethics, very popular during the Renaissance. 
	  The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
	  </div>
   </div>
   <br /><br />
   <div class="section">
      <h1>Third Section</h1>
      <a onclick="printSection('section3')">
	  <img title="Print Third Section" src="images/printicon.png" alt="" />
	  </a>
      <div id="section3">It is a long established fact that a reader will be distracted by 
	  the readable content of a page when looking at its layout. The point of using Lorem
	  Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using
	  'Content here, content here', making it look like readable English. Many desktop publishing 
	  packages and web page editors now use Lorem Ipsum as their default model text, and a search 
	  for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have 
	  evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</div> </div> </div> </body>
</html>




Related Articles

How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP
PHP SplFileObject Standard Library
Simple File Upload Script in PHP
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL
Php file based authentication
Simple PHP File Cache
How to get current directory, filename and code line number in PHP
Preventing Cross Site Request Forgeries(CSRF) in PHP
PHP code to send email using SMTP
Simple pagination in PHP
PHP Connection and File Handling on FTP Server




Most Popular Development Resources
Retrieve Data From Database Without Page refresh Using AJAX, PHP and Javascript
-----------------
Characteristics of a Good Computer Program
-----------------
How to get data from XML file in PHP
-----------------
PHP code to send email using SMTP
-----------------
PHP Create Word Document from HTML
-----------------
Hypertext Transfer Protocol Overview
-----------------
PHP MySQL PDO Database Connection and CRUD Operations
-----------------
Create Dynamic Pie Chart using Google API, PHP and MySQL
-----------------
How to encrypt password in PHP
-----------------
Splitting MySQL Results Into Two Columns Using PHP
-----------------
How to get current directory, filename and code line number in PHP
-----------------
Dynamically Add/Delete HTML Table Rows Using Javascript
-----------------
Get current visitor\'s location using HTML5 Geolocation API and PHP
-----------------
How to Sort Table Data in PHP and MySQL
-----------------
PHP MYSQL Advanced Search Feature
-----------------
Simple star rating system using PHP, jQuery and Ajax
-----------------
Simple pagination in PHP with MySQL
-----------------
Fibonacci Series Program in PHP
-----------------
How to add multiple custom markers on google map
-----------------
jQuery loop over JSON result after AJAX Success
-----------------
PHP user registration and login/ logout with secure password encryption
-----------------
Submit a form data using PHP, AJAX and Javascript
-----------------
Recover forgot password using PHP7 and MySQLi
-----------------
Php file based authentication
-----------------
PHP Secure User Registration with Login/logout
-----------------
jQuery File upload progress bar with file size validation
-----------------
Polling system using PHP, Ajax and MySql
-----------------
Simple PHP File Cache
-----------------
How to generate QR Code in PHP
-----------------
SQL Injection Prevention Techniques
-----------------
Simple File Upload Script in PHP
-----------------
PHP User Authentication by IP Address
-----------------
Calculate the distance between two locations using PHP
-----------------
To check whether a year is a leap year or not in php
-----------------
Preventing Cross Site Request Forgeries(CSRF) in PHP
-----------------
CSS Simple Menu Navigation Bar
-----------------
PHP Server Side Form Validation
-----------------
Detect Mobile Devices in PHP
-----------------
Simple way to send SMTP mail using Node.js
-----------------
Set and Get Cookies in PHP
-----------------
Date Timestamp Formats in PHP
-----------------
Get Visitor\'s location and TimeZone
-----------------
Convert MySQL to JSON using PHP
-----------------
Simple Show Hide Menu Navigation
-----------------
PHP Programming Error Types
-----------------
PHP Sending HTML form data to an Email
-----------------
How to print specific part of a web page in javascript
-----------------
Driving route directions from source to destination using HTML5 and Javascript
-----------------
Google Street View API Example
-----------------
How to select/deselect all checkboxes using Javascript
-----------------
How to add google map on your website and display address on click marker
-----------------
PHP Getting Document of Remote Address
-----------------
PHP Connection and File Handling on FTP Server
-----------------
File Upload Validation in PHP
-----------------
R Plot Types
-----------------


Most Popular Blogs
Most in demand programming languages
Best mvc PHP frameworks in 2019
MariaDB vs MySQL
Most in demand NoSQL databases for 2019
Best AI Startups In India
Kotlin : Android App Development Choice
Kotlin vs Java which one is better
Top Android App Development Languages in 2019
Web Robots
Data Science Recruitment of Freshers - 2019


Interview Questions Answers
Basic PHP Interview
Advanced PHP Interview
MySQL Interview
Javascript Interview
HTML Interview
CSS Interview
Programming C Interview
Programming C++ Interview
Java Interview
Computer Networking Interview
NodeJS Interview
ExpressJS Interview
R Interview





General Knowledge

listen
listen
listen
listen
listen
listen
listen
listen
listen


Learn Popular Language

listen
listen
listen
listen
listen

Blogs

  • Jan 3

    Stateful vs Stateless

    A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

  • Dec 29

    Best programming language to learn in 2021

    In this article, we have mentioned the analyzed results of the best programming language for 2021...

  • Dec 20

    How is Python best for mobile app development?

    Python has a set of useful Libraries and Packages that minimize the use of code...

  • July 18

    Learn all about Emoji

    In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

  • Jan 10

    Data Science Recruitment of Freshers

    In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...

Follow us

  • etutorialspoint facebook
  • etutorialspoint twitter
  • etutorialspoint linkedin
etutorialspoint youtube
About Us      Contact Us


  • eTutorialsPoint©Copyright 2016-2021. All Rights Reserved.