user123_456
user123_456

Reputation: 5805

How to insert already displayed div in print function

How can I insert div that is already shown on web page in the print function.

This is the function:

function printHTML(input){
  var iframe = document.createElement("iframe");
    document.body.appendChild(iframe);

  iframe.contentWindow.document.write(input);
  iframe.contentWindow.print();
  document.body.removeChild(iframe); 
}

printHTML('<h1>Test!</h1>');

But I need to put inside something that is already shown on the webpage

Upvotes: 0

Views: 150

Answers (2)

Anoop
Anoop

Reputation: 23208

You can pass clone of your div and appendf in iframe body. Modified code: jsfiddle

function printHTML(clonedDive){
  var iframe = document.createElement("iframe");
    document.body.appendChild(iframe);

  iframe.contentWindow.document.body.appendChild(clonedDive);
  iframe.contentWindow.print();

  document.body.removeChild(iframe); 
}

printHTML( document.getElementById("divid").cloneNode(true));​

Upvotes: 1

martinwnet
martinwnet

Reputation: 581

You want to use document.getElementById() to get whatever you need.

E.g. to get some div on your page:

var someDiv = document.getElementById("divId");
printHTML(someDiv);

Upvotes: 0

Related Questions