April 29, 2012

jquery re-attach div on pop up window close

Question by Anna Riekic

I’m using jQuery to open a popup window which works great but I’ve also have it that the #content div detaches from “parent” page …

$('.newWindow').click(function(ev){

window.open('popout.html','pop out','width=400,height=345');
ev.preventDefault();
$('#content').detach();
return false;

});

link used:

<a href="popout.html" rel="0" class="newWindow">Pop Out Window</a>

How can I “re-attach” that div when pop up window is closed?

I’ve tried many of the answers I found here on SO but none seem to work.

Update:

It seems my browser cached the popup window as when I viewed the source (via right click view source) I noticed any changes I made IE the new JS code was not there so closed browser and reopened and hey presto code edits where there…

have temporarily gone for:

window.onunload = function(){
  window.opener.location.reload();
};

which refreshes the parent page on child close but would still prefer a reattach approach.

Answer by Starx

Create a temporary variable to store the div before detaching.

var content; // temp storage

$('.newWindow').click(function(ev){

     window.open('popout.html','pop out','width=400,height=345');
     ev.preventDefault();
     content = $('#content');
     $('#content').detach();
     return false;
});

When you need to reattach this in the DOM again, just use .appendTo()

content.appendTo($("#whereToReAttach"));
...

Please fill the form - I will response as fast as I can!