Print the contents of a DIV
Slight changes over earlier version - tested on CHROME
function PrintElem(elem)
{
var mywindow = window.open('', 'PRINT', 'height=400,width=600');
mywindow.document.write('<html><head><title>' + document.title + '</title>');
mywindow.document.write('</head><body >');
mywindow.document.write('<h1>' + document.title + '</h1>');
mywindow.document.write(document.getElementById(elem).innerHTML);
mywindow.document.write('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10*/
mywindow.print();
mywindow.close();
return true;
}
Print div id=printarea/div only?
Here is a general solution, using CSS only, which I have verified to work.
@media print {
body * {
visibility: hidden;
}
#section-to-print, #section-to-print * {
visibility: visible;
}
#section-to-print {
position: absolute;
left: 0;
top: 0;
}
}
Alternative approaches aren't so good. Using display
is tricky because if any element has display:none
then none of its descendants will display either. To use it, you have to change the structure of your page.
Using visibility
works better since you can turn on visibility for descendants. The invisible elements still affect the layout though, so I move section-to-print
to the top left so it prints properly.
I want to print only the specific div content and email results from HTML/JS page?
You need to use actual div .final-results
in the CSS
/ @media print{}
to show the results tables only in the window.print()
command when you click on Print to PDF
Also, you can add the custom CSS
to your PDF
as your wish to
Edit: I have added querySelector
method to add the innerText
of your .final-results
div in to href
of so you can use that for emailing
as well by clicking send to Email
Live Demo:
function displayRadioValue() {
let section1 = document.querySelectorAll('.section-1 > input[type="radio"]')
let section2 = document.querySelectorAll('.section-2 > input[type="radio"]')
let section1Total = 0
let section2Total = 0
let section1Question = 0
let section2Question = 0
let finalResults = document.querySelector('.final-results')
let result1 = ''
let result2 = ''
finalResults.innerHTML = ''
//Section 1
section1.forEach(function(radio, index) {
if (radio.checked) {
section2Question++
section1Total += +radio.value
}
})
//Section 2
section2.forEach(function(radio, index) {
if (radio.checked) {
section1Question++
section2Total += +radio.value
}
})
//Final Results and validation
if (section1Total > 0 && section2Total > 0) {
finalResults.innerHTML += genTable(section1Question, section1Total, 1)
finalResults.innerHTML += genTable(section2Question, section2Total, 2)
document.getElementById("control").style.display = "block";
document.getElementById("toemail").href += document.querySelector(".final-results").innerText;
} else {
finalResults.innerHTML = 'Snap! Please select the atleast one survey question from each section '
}
}
function genTable(ques, total, section) {
var result = "<b>Section " + section + ":</b><br>"
var tr = "<tr><th>" + total + "</th><th>" + ((total / (ques * 3)) * 100).toFixed(2) + "</th></tr>"
result += "<table><thead><tr><th>Total Score</th><th>Percentage</th></tr></thead><tbody>" + tr + "</tbody></table>"
return result
}
@media print {
body * {
visibility: hidden;
}
.final-results * {
visibility: visible;
}
.final-results {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
}
}
table,
table tr th,
table tr td {
border: 1px solid black;
}
<p>
Select a radio button and click on Submit.
</p>
<div class="section-1">
<h2>Section 1</h2>
question 1:
<input type="radio" name="question1" value="1">1
<input type="radio" name="question1" value="2">2
<input type="radio" name="question1" value="3">3
<br> question 2:
<input type="radio" name="question2" value="1">1
<input type="radio" name="question2" value="2">2
<input type="radio" name="question2" value="3">3
<br> question 3:
<input type="radio" name="question3" value="1">1
<input type="radio" name="question3" value="2">2
<input type="radio" name="question3" value="3">3
</div>
<div class="section-2">
<h2>Section 2</h2>
question 1:
<input type="radio" name="question4" value="1">1
<input type="radio" name="question4" value="2">2
<input type="radio" name="question4" value="3">3
<br> question 2:
<input type="radio" name="question5" value="1">1
<input type="radio" name="question5" value="2">2
<input type="radio" name="question5" value="3">3
<br> question 3:
<input type="radio" name="question6" value="1">1
<input type="radio" name="question6" value="2">2
<input type="radio" name="question6" value="3">3
<br> question 4:
<input type="radio" name="question7" value="1">1
<input type="radio" name="question7" value="2">2
<input type="radio" name="question7" value="3">3
</div>
<br>
<div class="final-results"></div>
<br>
<button type="button" onclick="displayRadioValue()">
Submit
</button>
<div id="control" style="display: none"><a id="toemail" href="mailto:youremail@domain.com?subject=Survey response&body=">Send to
email</a> <button onclick="window.print();">Send to PDF</button></div>
How to print an specific DIV with its CSS
You can select all other elements and hide them from the page (excluding the target with the use of the :not()
CSS pseudo class), then call window.print()
to print the page:
btn.addEventListener('click', function(){
document.body.querySelectorAll(':not(.red)').forEach(e => e.style.display = "none");
window.print();
})
div{
height:100px;
border:1px solid;
}
.red{
background-color:red;
}
.blue{
background-color:blue;
}
<div class="red">
red
</div>
<div class="blue">
blue
</div>
<button id="btn">Print Red div</button>
how to show the contents of a div only in the print file?
You can use css @media print
This will hide all body elements and show only the div #printThis
and all its children.
@media print {
body * {
visibility: hidden;
}
#printThis, #printThis * {
visibility: visible;
}
#printThis {
width : 100%;
height : auto;
position: absolute;
left: 0;
top: 0;
}
}
js (No need to modify/hide elements. CSS will handle it)
function printDiv( printThis ) {
window.print();
}
HTML:
<div id="printThis">
content
</div>
<input type="button" onclick="printDiv('printThis')" value="PRINT" />
print content of a div except some element in it, doesnt work
Actually the newly created window using window.open
does not have any style attached to it.
Here is a snapshot
Style can be added to it by adding inline style in the following code
printWindow.document.write('<html><head><style media="print">' +
'p {color: green;}.noprint {display: none !important;}</style><title>DIV Contents</title>');
javascript to print contents of only specific div
You require to create new style sheet print.css and set CSS media=print
for example :
<style media="screen">
.noPrint{ display: block; }
.yesPrint{ display: block !important; }
</style>
<style media="print">
.noPrint{ display: none; }
.yesPrint{ display: block !important; }
</style>
for more detail : Print a doument with CSS
How to print selected div instead complete page
Try this
<style type="text/css">
@media print
{
body * { visibility: hidden; }
.div2 * { visibility: visible; }
.div2 { position: absolute; top: 40px; left: 30px; }
}
</style>
Related Topics
Need to Load Image as Modal on Click in Bootstrap CSS
Detect When Scrolling Has Finished When Using 'Scroll-Behavior: Smooth'
How to Add an Icon to the Options in React-Select
Handle Webpack CSS Imports When Testing with Mocha and Babel
CSS JavaScript Style Sheet Disabled
Webkit Scrollbar Dynamic Styling
CSS Select All Previous Siblings for a Star Rating
React Onclick Add Class to Clicked Element But Remove from the Others
Is There a Way I Can Force Chrome to Do Subpixel Rendering for a Slow Translation
Svg Resizes on Hover in Safari Only
Width of the Parent Container with Jquery
Change Image Onclick for a Set Amount of Time
JavaScript .Replace Alternative
Find Unused CSS Rule and Js Script in a Web Project
Find Next Cell Contained in Sibling Row with Queryselector
Send a Notification from JavaScript in Uiwebview to Objectivec
How to Make Each Letter in Text a Different Random Color in JavaScript