Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to print multiple nested divs

    Hi guys ,, I am new here

    could you please help me in this:

    I have multiple divs something like this

    PHP Code:
    <div id="div1">
    <
    div id="div2">
    <
    div id="div3">
    ....
    </
    div>
    </
    div

    I want JavaScript code to print div1 with all nested divs as well

    I tried using the following code but it only print div1 content


    PHP Code:
    var printContent document.getElementById('div1');

    var 
    windowUrl 'about:blank';

    var 
    uniqueName = new Date();

    var 
    windowName 'Print' uniqueName.getTime();

    var 
    printWindow window.open(windowUrlwindowName'left=50000,top=50000,width=200,height=200');

    printWindow.document.write(printContent.innerHTML);

    printWindow.document.close();

    printWindow.focus();

    printWindow.print();

    printWindow.close(); 

    so please tell me how to do such operation ??

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Why don't you *start* by seeing if your printWindow *DISPLAYS* the nested DIVs???

    That is, change this line:
    Code:
    var printWindow = window.open(windowUrl, windowName, 'left=50000,top=50000,width=200,height=200');
    to simply this:
    Code:
    var printWindow = window.open(windowUrl, windowName );
    And then (for now) remove the two lines
    Code:
    printWindow.print(); 
    printWindow.close();
    What does that popup window look like? Does it contain all the nested div content?

    If not, then of course it can't be printed.

    Time to diagnose why getting the innerHTML of div1 isn't enough.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Aug 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Old Pedant,

    This thread is close with my thread (posted in ASP forum) regarding save to database the DIV contents. Taking into account the print mode, would be possible print to a file, like .pdf?

    regards

    Jan Lee

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Printing to a PDF file requires a component. Which costs money. But yes, you could.

    You could certainly print to a text file, of course.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    Aug 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts
    How I do it?

    Well, I am trying (still) solve that problem. To save into a text file could be fine.

    Regards

    Jan Lee

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Go back to the ASP thread. This doesn't belong in the JavaScript forum.

    As I said there, you *could* save the <div> contents in the database, but why? It would be unformatted and nearly useless for any kind of searching at a later time. Explain the purpose of this, in that ASP thread.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •