Print the content of a div element using JavaScript

To print the content of div in JavaScript, first store the content of div in a JavaScript variable and then the print button is clicked. The contents of the HTML div element to be extracted. Then a JavaScript popup window is created and the extracted contents of the HTML div elements are written to the popup window and finally the window is printed using the JavaScript Window Print command.

Example 1: This example uses JavaScript window print command to print the content of div element .

               Div contents are 
'

Output:

Example 2: This example uses the JavaScript window print command to print the content of div element.

               Div contents are 
'

Output:

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples .

Like Article -->

Please Login to comment.

Similar Reads

How to copy the content of a div into another div using jQuery ?

Given an HTML document containing some div element and the task is to copy a div content into another div as its child using jQuery. There are two approaches to solve this problem that are discussed below: Approach 1: First, select the div element which needs to be copied into another div element.Select the target element where the div element is c

2 min read How to clear all div's content inside a parent div ?

In this article, we are given an HTML document containing div elements and the task is to remove the existing HTML elements using jQuery. To accomplish this task, we can use the following methods: Table of Content jQuery remove() methodjQuery empty() methodjQuery html() methodjQuery remove() methodThe remove() method in jQuery is used to remove the

2 min read How to Make a Child Div Element Wider than Parent Div using CSS ?

When the child div element has a fixed width that is wider than the parent div element, it will display overflow outside of the parent element. Because the overflow is hidden by default, one approach is to utilize the overflow attribute to enable it on an element. Another approach includes setting the position property of the parent div element to

2 min read How to overlay one div over another div using CSS

Creating an overlay effect simply means putting two div together at the same place but both the div appear when needed i.e while hovering or while clicking on one of the div to make the second one appear. Overlays are very clean and give the webpage a tidy look. It looks sophisticated and is simple to design. Overlays can create using two simple CS

2 min read How to clear the content of a div using JavaScript?

JavaScript provides the functionality of clearing the content of div. There are two methods to perform this function, one by using the innerHTML property and other by using the firstChild property and removeChild() method. Below are the approaches used to clear the content of a div using JavaScript: Table of Content Using innerHTML Property Using f

3 min read How to force child div to be 100% of parent div's height without specifying parent's height?

When designing a website, you may want to create a layout where a child div element fills up the height of its parent div element. However, you may not know the exact height of the parent div element and you don't want to specify it. In this article, we will explore different approaches to force a child div element to be 100% of its parent div elem

3 min read How to center a div within another div?

The div tag is used to construct a division or section of an HTML document in which other elements of HTML is placed and that division/section works like a container whose CSS styling can be done as a single unit or javascript can be used to perform various tasks on that container. Syntax: <div> <h3>'This is a div of the web page.>/h

4 min read How to center a div on the edge of another div in SASS ?

In this article, we will see how to centre a div on the edge of another div using SASS. SASS is an extension of CSS that stands for Syntactically Awesome Style Sheets. It helps us to create things like variables, nested rules, etc. So that we can reuse the code. Centering a div on the edge of another div is not a difficult task. We can achieve it b

4 min read How to make Parent Div Activate Styling of Child Div for Hover and Active ?

In this article, we will see how to make parent div activate styling of child div for hover and active. We can do this by using Pseudo-classes like hover and active. Approaches to make parent div activate styling of child div for hover and activeUsing Pure CSS: with pseudo-classesUsing CSS and JavaScript: without pseudo-classesMethod 1: Using Pure

2 min read How to make div fixed after scroll to that div ?

In this article, we will see how to make a div fixed after you scroll to it by using the position property with a sticky value. This effect is generally used on websites when an important message needs to be shown. Set the property position to sticky to create elements that behave as fixed positions whenever the user scrolls to that position. Fixin

4 min read How to Overlay/Hover Div in Flexbox Container Div?

In CSS, overlay/hover effects can be achieved by using Flexbox containers to center content and applying styles to reveal overlays on hover. By positioning elements absolutely within a Flexbox container, overlays can cover the entire content area. These are the following approaches: Table of Content Using ::before Pseudo-elementUsing an Overlay Div

3 min read How to align content of a div to the bottom using CSS ?

Basic property of CSS: position: The position property specifies the type of positioning method used for an elements. For example static, relative, absolute and fixed.bottom: The bottom property affects the vertical position of a positioned element. This property has no effect on non-positioned elements.left: The left property affects the horizonta

2 min read How to dynamically get the content width of a div using AngularJS ?

In this article, we will see how to dynamically get the content width of a div using AngularJS, along with understanding its basic implementation through the examples. The content width of a div can dynamically get using clientWidth and scrollWidth properties depending upon the user's requirement. If a user wants to know the space required by actua

2 min read How to detect and change the content/style of a div using jQuery ?

The changes to the html/text of a div can be detected using jQuery on() method. The on() is used to attach one or more event handlers for the selected elements and child elements in the DOM tree. The on() method is the replacement of the bind(), live() and delegate() method from the jQuery version 1.7. Syntax: $(selector).on(event, childSelector, d

2 min read How to dynamically get the content height of a div using AngularJS ?

In this article, we will see how to get the height of the content of a <div> dynamically using Javascript, & will understand its implementation through the illustrations. The content height of a div can dynamically get using the clientHeight property and scrollHeight property depending upon the user's requirement. If a user wants to know

3 min read How to call a function when content of a div changes using jQuery ?

The task is to call a function when the content of a div is changed. You can achieve this task by using the in-built method known as .change(). In simple words whenever the field is been changed we call a function that will throw a pop-up. .change(): This method will only trigger when some change occurs in the element or field. It accepts an option

2 min read How to make a div vertically scrollable without content using CSS ?

In this article, we will see how we can create a vertical scrollable section using CSS. HTML code is used to create the basic structure of the sections and CSS code is used to set the style, HTML Code: Create an HTML div element with the class container.Created three more nested div with class boxWrite some content inside each div with the class bo

2 min read How to Set Div Width to Fit Content using CSS?

The div can be managed to set the whole screen width or the width according to the need. In this article, we are going to discuss how can we set a div width to fit content using CSS. we will discuss different approaches and code examples. Below are the approaches to set div width to fit content using CSS: Table of Content Using Default CSSUsing inl

3 min read How to make div width expand with its content using CSS ?

Given an HTML document and the task is to make div width expand with its content using CSS. To do this, the width property is used to set the width of an element excluding padding, margin and border of element. The width property values are listed below: Syntax: width: length|percentage|auto|initial|inherit;Property Values: width: auto; It is used

4 min read How to make div height expand with its content using CSS ?

To make a div's height adapt to its content using CSS, use `height: auto;` or skip setting a fixed height. This ensures the div adjusts dynamically to different content sizes, promoting a flexible layout. Avoid conflicting styles that might restrict the height, allowing you to create a responsive design that seamlessly accommodates diverse content

3 min read How to append data to <div> element using JavaScript ?

To append the data to <div> element we have to use DOM(Document Object Model) manipulation techniques. The approach is to create a empty <div> with an id inside the HTML skeleton. Then that id will be used to fetch that <div> and then we will manipulate the inner text of that div. Syntax: document.getElementById("div_name").innerT

1 min read How to rotate an HTML div element 90 degrees using JavaScript ?

An element can be rotated 90 degrees by using the transform property. This property is used to move, rotate, scale, and others to perform various kinds of transformation to elements. ApproachThe rotate() transformation function can be used as the value to rotate the element. It takes one parameter that defines the rotation angle. The rotation angl

2 min read How to remove specific div element by using JavaScript?

Removing a specific `<div>` element with JavaScript is crucial for dynamic web content management. Whether updating information, responding to user actions, or optimizing performance by cleaning the DOM, efficient DOM manipulation is essential. JavaScript offers diverse methods such as `removeChild`, `parentNode.removeChild`, `remove`, and jQ

3 min read How to hide div element by default and show it on click using JavaScript and Bootstrap ?

We have to first hide the div and when a user will click the button we need to display that hiden div on the screen. It is helpul for creating any type of form. There are two methods to solve this problem which are discussed below: Table of Content Using .show() methodUsing .toggle() methodApproach 1: Using .show() methodSet display: none property

2 min read How to Filter a DIV Element Based on its Class Name using JavaScript?

Div Element can be filtered based on class name for displaying specific content using JavaScript. Here, we will explore two different approaches to filtering a DIV element. Table of Content Using querySelectorAll and classListUsing getElementsByClassNameUsing querySelectorAll and classListIn this approach, we are using querySelectorAll to select al

3 min read How to Scroll to an Element Inside a Div using JavaScript?

To scroll to an element within a div using JavaScript, set the parent div's scrollTop`to the target element's offsetTop. This method allows smooth navigation within a scrollable container. Below are the methods to scroll to an element inside a Div using JavaScript: Table of Content Using scrollIntoView( ) method Using scroll( ) method Using scrollT

3 min read How to use a div as content for Twitter's Popover?

Twitter’s Popover is a bootstrap component somewhat similar to tool-tip where the highlighting difference is that it is triggered whenever the user clicks on the specified selector. Before we use <div> as the content inside the popover, let us have a look at the DOM structure of the popover. Syntax:$([selector]).popover(< html:true; title:[he

2 min read How to check if the clicked element is a div or not in JavaScript ?

Given an HTML document, which contains many elements. The task is to determine whether the clicked element is DIV or not with the help of JavaScript. Two approaches are discussed here, first approach uses the tagName property of element and second uses instanceOf operator to search for DIV. Approach 1: Use element.tagName property, which returns th

2 min read How to Underline Text of a Div Element in JavaScript ?

Underlining text of a div can help to emphasize certain words or phrases and make them stand out from the rest of the content on the page. To underline text in a div element using JavaScript, you can use the style property of div to text-decoration. Text Decoration: The text-decoration property is used to specify whether the text should be Underlin

3 min read How to print the content of current window using JavaScript ?

The task is to print the content of the current window by using the window.print() method in the document. It is used to open the Print Dialog Box to print the current document. It does not have any parameter value. Syntax: window.print()Parameters: No parameters are required Returns: This function does not return anything Example: This example rep