Mohammad Saberi
Mohammad Saberi

Reputation: 13166

write contents in a certain part of new window using jQuery

I have this codes in my projects to open a new window to show and then, print the result table from my first window:

$('a.print_btn').click(function(e) {
    var elementId = $(this).attr('id');
    var elementToPrint = $("#report_" + elementId).html();
    var printWin = window.open("print.php?id="+elementId, "Print");
    $(printWin.document.body).html(elementToPrint);
}); 

In this code, #report_elementId points to a fieldset that contains my table information. If I use the code below I can see all table contents:

alert(elementToPrint);

So everything is ok so far. But the last line of code does not work and anything don't write in printWin window. I don't know why :(

I used Write Content To New Window with JQuery for it, but it does not work for me. Help me please.

By the way, I need to write contents to a certain div (id="myDiv"). How can I do it?

Upvotes: 1

Views: 705

Answers (2)

rene
rene

Reputation: 42483

Try this:

  var printWin = window.open("print.php?id="+elementId, "Print");
  $(printWin.document).ready( function() {
     $('div#myDiv').html(elementToPrint); 
  }

I assume print.php?id=[something] does work, ie not ending in a 500 or a 404 error...

As I'm uncertain in what context the ready callback is executed let's try to be more explicit:

  var printWin = window.open("print.php?id="+elementId, "Print");
  $(printWin.document).ready( function() {
     $(printWin.document).find('div#myDiv').html(elementToPrint); 
  }

Upvotes: 2

Dion
Dion

Reputation: 3345

Try

printWin.body.innerHTML = elementToPrint;
document.getElementById('myDiv').innerHTML = elementToPrint;

Upvotes: 0

Related Questions