Home >>jQuery Tutorial >jQuery wrap() Method

jQuery wrap() Method

jQuery wrap() Method

jQuery wrap() method in jQuery is used to wraps specified HTML element around each selected element.

Syntax:
$(selector).wrap(wrappingElement,function(index))

Parameter Values

Parameter Description
wrappingElement It is a required parameter and is used to specifies what HTML elements to wrap

Possible values:

  • jQuery objects
  • HTML elements
  • DOM elements
function(index) It Is optional and is used to returns the wrapping element of a specifies function
  • index – Used to returns the index position of the selected element
Here is an Example of jQuery wrap() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".btn1").click(function(){
$(".txt1").wrap("<div></div>");
});
});
</script>
<style>
div{
background-color:orange;
}
</style>
</head>
<body>
<p class="txt1">This is First paragraph.</p>
<p class="txt1">This is Second paragraph.</p>
<button class="btn1">Click here to Wrap a div element</button>
</body>
</html>