Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrap content in a Div with javascript without iframe refreshing

Basically I want to wrap some content on my page in a div.

I can do this using the wrap function in jQuery but my issue is that I have a few iframes in my content which contain advertising and when you wrap the content it refreshes the iframes.

From what I can tell this is because behind the scenes it is cloning the content to be wrapped into the new div.

html

<div class="content">
   <iframe src="http://something.com/ads"></iframe>
</div>

js

$(content).wrap('awesome_container');

Ideally I would like to just slot in some open div tags before the content div and a few close div tags afterwards but I can't seem to add any divs to the page without it adding close tags.

Does anyone know of a way around this issue?

Cheers

like image 701
Marklar Avatar asked Nov 04 '22 07:11

Marklar


1 Answers

Instead of removing the html or making a copy of it, see if you can get the desired result using jQuery's detach() This will also allow whatever events you have associated with this element and its children to still function afterwards. It may be enough to preserve the iFrame object instead of killing it off.

var orphan = $(selector).detach()
someParent.html(orphan);

Let me know if that helps :)

like image 137
C.S. Avatar answered Nov 10 '22 20:11

C.S.