jQuery | wrap() with Examples

The wrap() method is an inbuilt method in jQuery which is used to wrap the specified element around the selected element.

Syntax:

$(selector).wrap(element, function)

Parameters: This method accepts two parameters as mentioned above and described below:

  • element: It is required parameter which is used to specify the element to wrap around the selected elements.
  • function: It is optional parameter which is used to specify the function which returns the wrapping element.

Return Value: This method returns the selected element with the specified changes made by wrap() method.

Below examples illustrate the wrap() method in jQuery:

Example 1: This example does not accept optional parameter.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>The wrap() Method</title>
        <script src=
        </script>
          
        <!-- jQuery code to show the working of this method -->
        <script>
            $(document).ready(function() {
                $("button").click(function() {
                    $("p").wrap("<div></div>");
                });
            });
        </script>
        <style>
            div {
                background-color: lightgreen;
                padding: 20px;
                width: 200px;
                font-weight: bold;
                height: 60px;
                border: 2px solid green;
            }
        </style>
    </head>
       
    <body>
        <!-- click on this paragraph and see the change -->
        <p>Welcome to GeeksforGeeks!</p><br>
        <button>Click Here!</button>
    </body>
</html>

chevron_right


Output:
wrap method

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>The wrap Method</title>
        <script src=
        </script>
          
        <!-- jQuery code to show the working of this method -->
        <script>
            $(document).ready(function() {
                $("button").click(function() {
                    $("p").wrap(function() {
                        return "<div></div>"
                    });
                });
            });
        </script>
        <style>
            div {
                background-color: lightgreen;
                padding: 20px;
                width: 200px;
                font-weight: bold;
                height: 60px;
                border: 2px solid green;
            }
        </style>
    </head>
    <body>
        <!-- click on this paragraph and see the change -->
        <p>Welcome to GeeksforGeeks!</p><br>
        <button>Click Here!</button>
    </body>
</html>

chevron_right


Output:
wrap method



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.