0

How do I add a <div> tag before <iframe> loads, using javascript? For example:

<div><iframe>Text</iframe></div>
1
  • Are you using jQuery or any js DOM tools? Commented Feb 25, 2016 at 16:48

3 Answers 3

2

I hope you have jQuery and not going for pure js.

$('iframe').wrap('<div></div>');
Sign up to request clarification or add additional context in comments.

Comments

0

You can try

$('iframe').contents().bind('load', function () {
    $('iframe').appendTo('div');
});

Comments

0

If you're using jQuery, and the following is your HTML...

<main id="content"><iframe>Text</iframe></main>

... then you could use ...

$('iframe').wrap( "<div></div>");

... or ...

$( "iframe" ).wrapAll( "<div></div>");

... or ...

$('#content').wrapInner( "<div></div>");

... to turn it into ...

<main id="content"><div><iframe>Text</iframe></div></main>

Each of these three statements do pretty much the same thing in this specific context.


Documentation :

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.