1

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?

2 Answers 2

2

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); 
  }
Sign up to request clarification or add additional context in comments.

6 Comments

rene, $('myDiv').html(elementToPrint); does not work for me :( I don't know why. If I alert elementToPrint I can see all html contents, but I don't know why it can not be written
It seems that I don't have any access to $('myDiv'). I tried $('myDiv').css('background-color','#333'); too. But anything did not happen.
@Mohammad I added a second example to make sure selection runs the right context
but this line $(printWin.document).find('div#myDiv').html(elementToPrint); didn't work for me-@rene
@ubm ask a new question where you follow the guidance in minimal reproducible example to make sure we can see what is different between your case and this one. It could be a security policy, a jquery error, html markup that is wrong etc. There is barely enough info your comment to diagnose what could be wrong.
|
0

Try

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.