javascriptjqueryprintthis

How to print a div on a click of a button?


I'm using printThis by Jason day and on its config page it shows that you can trigger the plugin using:

$('selector').printThis();

Now when I do that I have noticed that this trigger is showing print box on Page load instead of clicking on a button.

How do I use this plugin and trigger it on a button instead of a default page load?

Usage: https://jasonday.github.io/printThis/


Solution

  • So in order to do that you need to add some listener for the click event of your button.

    Assume this is your HTML:

    <button id="myButton">Button</button>
    
    <div id="myPrintableContent">
      Some content to print
    </div>
    

    And this is your javascript code:

    $(document).ready(function() {
      $('#myButton').on('click', function () {
        $('#myPrintableContent').printThis();
      });
    });