ricardo
ricardo

Reputation: 621

Insert new element in the <body> with JQuery

I want some help for someone about how to insert a new element in a body of document using JQuery.

I´m recover the body with:

var a = $(editorFrame).contents().find('body').html();

Then, I need to verify that the body has the element <p></p>, with it doesn´t, I have to put this element after the first <DIV>. Here is my body

<body>
<div id="testeFooter" class="footer-element">
    <table id="footerDiv" class="mceItemTable" width="100%">
        <tbody>
            <tr>
                <td id="footerContent">
                    <b>Digite aqui o seu Rodap&#233;</b>
                </td>
            </tr>
        </tbody>
    </table>
</div>
<div id="testeHeader" class="header-element">
    <table id="headerDiv" class="mceItemTable" width="100%">
        <tbody>
            <tr>
                <td id="headerContent">
                    <b>Digite aqui o seu cabe&#231;alho</b>
                </td>
            </tr>
        </tbody>
    </table>
</div>
    <p>ESSSE MODELO TESTE</p>
</body>

I just need to know how I can put a new element after the first on the body.

Thank´s a bunch with you can help me!

Upvotes: 2

Views: 6754

Answers (3)

ricardo
ricardo

Reputation: 621

First, I want to thank all for help!

I resolve my problem just making this:

$(editorFrame).contents().find('body').append('<p><br></p>').html();    

The method prepend works too!

Upvotes: 0

zero7
zero7

Reputation: 1298

if(!$(a).find('p').length) {
        $('div').first().prepend('<p></p>');          
    }

Upvotes: 0

Tigra
Tigra

Reputation: 2631

What you mean after the first?

Anyway

var body = $(editorFrame).find('body');

body.prepend('<someelement>')

will insert right in the begining

If you need to insert for example after first element in the body you should use

body.find(':eq(0)').after('<someelement>')

Upvotes: 5

Related Questions