Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

window.print a div

@media print {
  body * {
    visibility: hidden;
  }
  #section-to-print, #section-to-print * {
    visibility: visible;
  }
  #section-to-print {
    position: absolute;
    left: 0;
    top: 0;
  }
}
Comment

window.print div

// It's really that simple. Make sure the dimensions of 'elem' 
// fit the width of the PDF layout you're looking for.
// FYI: IE, Edge, Chrome, Firefox, etc. all have different dimensions when 
// they print.
function PrintElem(elem: HTMLElement)
{
    var mywindow = window.open('', 'PRINT', 'height=400,width=600');

    mywindow.document.write(elem.innerHTML);

    mywindow.document.close(); // necessary for IE >= 10
    mywindow.focus(); // necessary for IE >= 10*/

    mywindow.print();
  	setTimeout(() => {
        // Without the timeout, the window seems to close immediately.
    	mywindow.close();    	
    }, 250);
}
Comment

window.print() specific div

function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}
Comment

window.print() specific div

<div id="printableArea">
      <h1>Print me</h1>
</div>

<input type="button" onclick="printDiv('printableArea')" value="print a div!" />
Comment

PREVIOUS NEXT
Code Example
Javascript :: js events 
Javascript :: href="javascript:void(null);" 
Javascript :: regexp object javascript 
Javascript :: nodejs await inside map 
Javascript :: safeareaview react native android 
Javascript :: How to empty a folder in Node.js 
Javascript :: javascript set to array 
Javascript :: generate a random id 
Javascript :: trigger input javascript 
Javascript :: datatable child rows without ajax 
Javascript :: js string to array 
Javascript :: set js 
Javascript :: rm -rf node_modules 
Javascript :: Take a Ten Minute Walk js 
Javascript :: how to delete element in list javascript 
Javascript :: multiple import react js 
Javascript :: react transition group 
Javascript :: add params to url vue 
Javascript :: how to sort an array of objects by two fields in javascript 
Javascript :: jquery telephone input mask 
Javascript :: install react router dom with npm 
Javascript :: change node version 
Javascript :: convert json string or parse 
Javascript :: react native strike through text 
Javascript :: nuxt axios middleware 
Javascript :: browserslisterror contains both .browserslistrc and package.json with browsers 
Javascript :: react-native-screens 
Javascript :: slide out navigation 
Javascript :: merge objects javascript 
Javascript :: object deep copy 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =