English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery wrap() method

jQuery HTML/CSS Methods

The wrap() method will wrap each selected element with the specified HTML element.

Useunwrap()Method to remove the parent element of the selected element.

Syntax:

Wrapper element:

$.wrap(wrappingElement)

Wrap elements using a function:

$.wrap(function(index))

Instance

Wrap the DIV element around each <p> element:

$("button").click(function(){
  $("p").wrap("<div></div>");
});
Test to see‹/›

This example uses document.createElement() to create a DIV element and wrap it around each <p> element:

$("button").click(function(){
  $("p").wrap(document.createElement("div"));
});
Test to see‹/›

Wrap elements using a function:

$("button").click(function(){
  $("p").wrap(function(){
      return document.createElement("div");
  });
});
Test to see‹/›

Toggle between wrapping and unwrapping elements:

$("#btn1").click(function(){
  $("p").wrap("<div></div>");
});
$("#btn2").click(function(){
  $("p").unwrap();
});
Test to see‹/›

Parameter Value

ParameterDescription
wrappingElementSpecify the structure to wrap each selected element

Possible Values:

  • HTML Element

  • DOM Element

  • jQuery Object

function(index)Specify a function that returns the wrapping element
  • index-Return the index position of the element in the collection

jQuery HTML/CSS Methods