tags:

views:

4529

answers:

3

i think i am not going about this quite right, being very new to jquery. i have a page with 3 recipes on it hidden. when a link to recipe A is clicked it opens in a modal. i want to be able to print just the content of the recipe. so i am opening a new window (nonmodal) and trying to write the recipe to it (depending on which recipe is chosen)

this is the code i am using

     $('a.new-window').click(function(){

    var recipe =  window.open('','RecipeWindow','width=600,height=600');
     $('#recipe1').clone().appendTo('#myprintrecipe');
    var html = "<html><head><title>Print Your Recipe</title></head><body><div id="myprintrecipe"></div></body></html>";
recipe.document.open();
recipe.document.write(html);
recipe.document.close();

      return false;

     });

but it returns an error. i think it is close but not quite right. the error is: missing ; before statement [Break on this error] var html = "Print...="myprintrecipe">";\n

A: 

The problem is in your string. Change it to this:

$('a.new-window').click(function(){

    var recipe =  window.open('','RecipeWindow','width=600,height=600');
     $('#recipe1').clone().appendTo('#myprintrecipe');
    var html = "<html><head><title>Print Your Recipe</title></head><body><div id=\"myprintrecipe\"></div></body></html>";
recipe.document.open();
recipe.document.write(html);
recipe.document.close();

      return false;

     });

The problem is that you had a " character inside the string, which ends the string. The script engine got confused and complained with the error message.

Marius
A: 

change :

var html = "<html><head><title>Print Your Recipe</title></head><body><div id="myprintrecipe"></div></body></html>";

to:

var html = "<html><head><title>Print Your Recipe</title></head><body><div id='myprintrecipe'></div></body></html>";

and take notes on the double quotes (")

Abu Aqil
+1  A: 

I think what you are looking for is the following:

jQuery(function($) {
    $('a.new-window').click(function(){

    var recipe =  window.open('','RecipeWindow','width=600,height=600');
    var html = '<html><head><title>Print Your Recipe</title></head><body><div id="myprintrecipe">' + $('<div />').append($('#recipe1').clone()).html() + '</div></body></html>';
    recipe.document.open();
    recipe.document.write(html);
    recipe.document.close();

    return false;

    });
});

Marius had a partially correct answer - you did have a javascript error in your html string, but the content wouldn't have been appended anyways even if the error didn't get thrown because you were trying to append the recipe before the #myprintrecipe div was even in the new window.

Hope this helps.

Tres
thanks tres! that was it, the string thing--DOH rookie mistake, i cant believe i missed that. the second issue was what i couldnt figure out, when to do the append... Thanks!!!
Okay, i am looking at this more carefully and dont really understand it. (although grateful it works)... is it possible to explain this part: $('<div />').append($('#recipe1').clone()).html() i dont get why / how the <div> is a selector in this case...
The div isn't a selector, it's html being passed to jQuery. When that happens, jQuery creates an object out of it that you can use in your chaining. What I've done with that bit of code is grab the "outer" HTML of the recipe object because if you just went $('#recipe1').html(), you would get everything inside of '#recipe1', but not '#recipe1' itself including its HTML. If you don't need to include '#recipe1' in the window, but only its HTML, then just go $('#recipe1').html(). The jQuery docs explains valid arguments here: http://docs.jquery.com/Core. See: "$(...) The jQuery Function:".
Tres
okay i get that now.