jQuery Method How to - Use wrap() to wrap a new div around all of the paragraphs








Question

We would like to know how to use wrap() to wrap a new div around all of the paragraphs.

Answer


<!--from  w  w w .j a v a  2 s .c  o m-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("p").wrapAll($(".doublediv"));
        });
    </script>
    <style>
      .doublediv { color:red; }
    </style>
  </head>
  <body>
          <p>World</p>
          <p>World</p>
          <div class="doublediv">asdf<div>
  </body>
</html>

The code above is rendered as follows: