ChevCast
ChevCast

Reputation: 59213

How to wrap all the body contents in a div with jquery?

In a JQuery plugin I'm writing I want to hide all the contents of the <body> tag by wrapping them in a div and calling .hide(). What is the best way to wrap existing DOM elements into a single parent element?

For example:

I want to turn this

<body>
    <h1>Hello World!</h1>
    <p>This is a sample document to be manipulated by a JQuery plugin!</p>
</body>

into this

<body>
    <div id="originalBodyContents">
        <h1>Hello World!</h1>
        <p>This is a sample document to be manipulated by a JQuery plugin!</p>
    </div>
</body>

Thanks!

Upvotes: 3

Views: 7489

Answers (2)

Turnip
Turnip

Reputation: 36672

This should do it...

$(document).ready(function() {
    $('body').wrapInner('<div class="new" />');
    $('.new').hide();
});

Here's a little fiddle to show something similar in action.

Upvotes: 11

Briguy37
Briguy37

Reputation: 8402

Any specific reason why you don't want to do the following?

$('body').hide();​​​​

Upvotes: 0

Related Questions