tags:

views:

14

answers:

2

hello
have a next question:
i.g. we have next html with structure:

<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
        <script type="text/javascript">
            $(document).ready(function(){
                $($("#global").attr("innerHTML")).wrap('<div name="wrapper"></div>');
            })
        </script>
    </head>
    <body>
        <div id="global">
            <div id="container">
                <div id="sub1">
                    <h1> hello world </h1>
                </div>
                <div id="sub2">
                    <h1> hello world again</h1>
                </div>
            </div>
            <div id="container3">
                    <h1> hello world again</h1>
            </div>
        </div>
    </body>
</html>

but wrap function, some why won't work on it, as I think, it happens, because innerHTML hasn't parent, so, how can i avoid this, and wrap few elements (which has 'one level' and common parent)?

+2  A: 

try

$("#global").contents().wrapAll('<div name="wrapper"></div>');

demo

Reigel
A: 

I'm not sure I understand exactly what you're asking, but if you want to wrap the containers, do:


$(document).ready(function(){
    $("#global > div").wrap('...');
})

If you want to wrap the subs, do:


$(document).ready(function(){
    $("#global > div > div").wrap('...');
})

If you want to wrap all divs, do:


$(document).ready(function(){
    $("#global div").wrap('...');
})
John