jQuery wrap() Method

The jQuery wrap() method can be used to wrap the selected element with the specified HTML element. You have to specify the HTML element to wrap around the selected element.

jQuery wrap() Method

Syntax

The syntax of this method is given below:-

Wrap Element
$(selector).wrap(htmlElement, function)

The above syntax, the function is the optional field to specify for wrapping the selected element.

Description of the Parameters

The description of the parameters is given below.

Parameter Name Description
selector Specify the element you want to select to wrap. Use tag name, class name, or id of the elements to select. It is a required field.
htmlElement Specify the HTML element you want to wrap the selected element. It is a required field.

jQuery wrap() Method to Wrap Selected Element

If you want to wrap the selected element, you have to specify the HTML element as given below. The example contains the paragraph that wrapped within the div element.

Example

Test it Live

Output

This is my paragraph.


You have to click the button given above to wrap the specified element. The above example showing the div element that also adds some CSS to the wrapped element.

Use Function to Wrap Selected Element

You can also use the function to wrap the selected element. However, the function is the optional parameter to specify. Use the below example to wrap the element with the function.

Example

Test it Live

Output

This is my paragraph.


The above example using the click event to wrap the element. When you click the button given above, the div element got wrapped around the paragraph element.

You may also like to read

I hope you like this tutorial on jQuery wrap() method. If you have any queries regarding the tutorial, please comment below.

References