jQuery wrap()

Introduction

Wrap a <div> element around each <p> element:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>//from   w  w  w  .j a  v  a 2 s  .c  o m
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").wrap("<div></div>");
  });
});
</script>
<style>
div{background-color: yellow;}
</style>
</head>
<body>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

<button>Wrap</button>

</body>
</html>

The wrap() method wraps specified HTML element(s) around each selected element.

$(selector).wrap(wrapping_Element,function(index))
Parameter
Optional
Description
wrapping_Element




Required.




HTML element(s) to wrap around each selected element
Possible values:
HTML elements
jQuery objects
DOM elements
function(index)

Optional.

sets a function that returns the wrapping element
index - the index position of the element in the set



PreviousNext

Related