How to print the contents of a page with Bootstrap and JQuery?

3

I would like to add a Bootstrap button to invoke the printing of content on a page, I got this example, but it works from a link, see: Example

The code places the link at the top of the page, I also saw an example here, on that Stackoverflow Example link, but I do not like to open the page with content again, simply click the button, call the print box and print as in the first example cited.

The code for the first example is this:

$(document).ready(function() {
    $('#container').prepend('Click aqui para imprimir');
    $('a#print').click(function() {
        window.print();
        return false;
    });
});
    
asked by anonymous 15.09.2015 / 17:56

1 answer

4

If your goal is to have a button that uses the bootstrap class and only use btn btn-primary for example, and if you do not want to open it again and just remove target_blank , I understand it would look something like this:

        document.getElementById('btn').onclick = function() {
          window.print();
        };
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script><linkrel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<body>
  <div id="sua_div">Essa é a DIV</div>
  <button id="btn" class="btn btn-primary">Clique para imprimir</button>
</body>
    
15.09.2015 / 18:07