Print page

I am trying to get a selective part of text (within a div tag) to get printed when a person presses the button “print this page”

so here is the code i got:

<script type="text/javascript">
function printDiv()
{
   var divToPrint=document.getEelementById('areaToPrint');
  newWin= window.open("");
  newWin.document.write(divToPrint.outerHTML);
  newWin.print();
  newWin.close();
}
</script>

the button:

<form><input type="button" value=" Print this page "
onclick="printDiv();return false;" /></form> 

but it won’t print. please help! how can i accomplish this?

In Firefox 3.6 I have to say that it works for me.


<html>
<body>
<div id="areaToPrint">
Something to print
</div>
<form><input type="button" value=" Print this page "
onclick="printDiv();return false;" /></form>
<script type="text/javascript">
function printDiv()
{
   var divToPrint=document.getElementById('areaToPrint');
  newWin= window.open("");
  newWin.document.write(divToPrint.outerHTML);
  newWin.print();
  newWin.close();
}
</script>
</body>
</html>

that’s the part that really confuses there is no error at all.

perhaps there is another method to print the content of a div tag?

Is getEelementById it?

edited that but doens’t print that content still :frowning:

Is it possible that your web browser is blocking the scripted creation of new windows?

don’t think so, its firefox, i tried it in IE and in Chrome, Opera and Flock. Same issue.

What happens a new window shows up with nothing in it and no print window shows up.

What error does the error console in the Tools menu show, and have you resolved the getElementById issue yet?

strange i do have <p> tags and <span> tags could that be throwing it off?

i don’t know if i can post the verbage code of it cause it is a strip club :X

How about linking us through to your page that has the trouble. There might be something else that we can see (no pun intended).
If you don’t wish for the link to be visible, you’re welcome to send it as a personal message.

pm sent

Ta.

I see that the page uses a class attribute for “areaToPrint”.
An id attribute will be necessary instead.

ok it works but now prints “undefined” :frowning:

Okay. How would you like me to help further. I see that the web page doesn’t appear to yet be updated with the correction, which makes it tricky for me to help much further.

yeah sorry about that, sec, filezilla crapped out on me.

updated now

It seems that outerHTML is pretty much an IE only property.

You can use the code from here to add outerHTML functionality for browsers that don’t understand it.
http://snipplr.com/view/5460/outerhtml-in-firefox/

Or, you can come up with a solution that uses innerHTML instead.

so how do i implement it? do I create a separate javascript tab for that code?

Just include the code as a part of the common script code that you use.

If you just want to use it on the page, then place it before the script that does the printing.

i see ok i just decided to use print the whole page script :stuck_out_tongue: