jQuery wrapAll() method wraps the specified HTML elements around all the selected elements.
Syntax:
$(selector).wrapAll(wrappingElement)
Wrapping Element:
- It is a compulsory parameter.
- It is used to specify the HTML elements to wrap around the selected elements.
- It can accept HTML elements, jQuery objects, and DOM elements as a value.
Example1:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").wrapAll("<em></em>"); }); }); </script> </head> <body> <p>Hello!</p> <p>Nice to see you.</p> <button>Wrap All</button> </body> </html>
Example2:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").wrapAll("<div></div>"); }); }); </script> <style> div{background-color: turquoise; } </style> </head> <body> <p>Hello</p> <p>Guys!</p> <button>Wrap All</button> </body> </html>