views:

15

answers:

4

I have a div that I want to write to a popup window (for printing).

I'm grabbing the contents of the div's I want on the page using jQuery's html() function like so:

function printOptions() {
        var printwindow = window.open('', 'Report', 'height=600,width=600');
        printwindow.document.write('<html><head><title>Report</title>');
        printwindow.document.write('</head><body>');
        printwindow.document.write($('#ReportHeader').html());
        printwindow.document.write($('#ReportData').html());
        printwindow.document.write('</body></html>');
        printwindow.document.close();
        printwindow.print();
        return true;
    }

However, before I document.write() the contents of the #ReportHeader and #ReportData div's, I would like to alter them a little.

Specifically, I would like to replace all textboxes with a simple span containing that textboxes value.

Something like:

$("input[type=text]").each(function() {
    $(this).replaceWith("<span>" + $(this).val() + "</span>"); 
});

How can I do that to just the contents of those divs without altering my existing page? I just want to modify what I'm going to be writing out to the print window. I don't think I can select on what the html() returns though, because it is just returning a string.

I do ~not~ want to modify the original page that is launching the popup. Just the contents of what I'm going to be writing to the popup.

Any ideas on how I could do this?

A: 

You can do whatever you want to the div via the regular jQuery selectors and modifiers.

var $reportHeader = $($('#ReportHeader').html());

// Remove some div
$reportHeader.find('div.to_remove').remove();

// Now use $reportHeader.html() to add the modified contents to printwindow
meagar
A: 

You can clone the content, then perform the replacements, then write it:

var cloned = $($('#ReportData').html());

Using the $('html string') function:

http://api.jquery.com/jQuery/#jQuery2

Ian Henry
+1  A: 

You can use .clone() to do it efficiently, like this:

printwindow.document.write(
  $('#ReportHeader').clone().find("input[type=text").each(function() {
    $(this).replaceWith("<span>" + $(this).val() + "</span>"); 
  }).end().html()
);

This gets a copy of the original #ReportHeader and its children so you can manipulate them how you want without modifying the original, as well as not enduring the expense of html string -> node conversion, which is compartively quite expensive.

Nick Craver
+1 - this is more semantically clear than my answer.
Ian Henry
I read the documentation on end() but I'm not sure I understand that. What is the point of end()? Thanks btw!
KingNestor
@KingNestor -`.end()` takes the chain back to the clone, otherwise you'd be getting the `.html()` for the input elements, instead of the entire `#ReportHeader` element, so whenever you do a traversal statement that changes which elements you're working with (like `.find()`), it'll take the chain back to working with the elements before that change happened.
Nick Craver
A: 

To replace all inputs with static span elements, you can do something like this..(not tested)

$(":input").each(function() {
    var parent = $(this).parent();
    var val = $(this).val();
    $(parent).remove($(this).append("<span>" + $(this).val() + "</span>")); 
});
Teja Kantamneni