-2

I am trying to print a text using javascript. I have searched n number of times like 'How to print using javascript?' the only way I can do it is window.print(). Is this the only way?? Or Is there any other way?? for example: Using jquery or something else?

georg
  • 211,518
  • 52
  • 313
  • 390
Mr ASquare
  • 391
  • 1
  • 8
  • 22
  • did any of the answers solve your problem ? – thatOneGuy Feb 19 '16 at 13:43
  • Why on earth this is down voted.. just because i did not accept any answer.. ?? This is not a lame question.. @thisOneGuy yeah your answer satisfied my question i will wait few more hours before accepting your answer.. I am waiting for someone who may provide an answer with something like a jquery print plugin(i don't know if this exists)... – Mr ASquare Feb 20 '16 at 05:35
  • why would you need a plugin ? It's 2 lines of code :/ What is it exactly you want to do ? – thatOneGuy Feb 20 '16 at 14:48
  • I am trying to print a barcode.. I have print information like '^XA^LH0,20^LL1...' and I want to invoke a printer from the browser itself by passing the printer information as input.. BTW mine is ZEBRA printer... as your answer is correct for the asked question I am accepting your answer. . – Mr ASquare Feb 22 '16 at 05:42
  • Yeah, I was going to say. Perhaps you should ask a more precise question of exactly what you want to happen, then you would get a more precise answer :) Let me know if you do and I'll take a look – thatOneGuy Feb 22 '16 at 09:04

3 Answers3

1
console.log(insert string here);

Above logs the string (text) to the console window (ctrl + shift + I, in Chrome).

Unless you actually mean print the text off from a printer.

This prints whole window :

     document.getElementById('printEverything').addEventListener("click", function (event) {
        window.print();}

//or JQuery

     $('.printEverything').click(function(){
        window.print();});

This prints selected area:

        document.getElementById('printCertainArea').addEventListener("click", function (event) {
             document.getElementById('selectedArea').print();
        }

//or JQuery

    $('.printCertainArea').click(function(){
         $("#outprint").print();
    });
thatOneGuy
  • 9,977
  • 7
  • 48
  • 90
0

try also document.write() and console.log()

L. Vadim
  • 539
  • 3
  • 12
0

To print output directly to your page you can do this:

<span id="ID" hidden>Enter you text here</span>

And when you want to show it use jQuery

$("#ID").show();
Ethan Fischer
  • 1,229
  • 2
  • 18
  • 30