jQuery wrapAll()

Introduction

Wrap a <div> element around all <p> elements:

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  ww w  .  j av a2 s  .co m
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").wrapAll("<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 a div element around all p elements</button>

</body>
</html>

The wrapAll() method wraps specified HTML element(s) around all selected elements.

$(selector).wrapAll(wrapping_Element)
Parameter
Optional
Description
wrapping_Element




Required.




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



PreviousNext

Related