Search code examples
jqueryhtml-parsingappendjquery-load

jQuery load a div wrapper before a div after page load


I want to a load a div to wrap around another div after page load and have CSS applied to that div.

This is the page:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <div id="body">

    </div>
</body>
</html>

I want to use jQuery that once the page is loaded to add a div wrapper around the body:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <div id="body2" style="xyx"><div id="body">

    </div></div>
</body>
</html>

There is a lot PHP, JavaScript and content between the body already and this is the solution I need for a quick fix. Is it possible?


Solution

  • You can use wrap method:

    $(function(){
        $('#body').wrap('<div id="body2" class="class"/>');
    })