Processing math: 100%

jQuery wrapInner()

jQuery wrapInner() method wraps all the HTML structure around the content of each element of the selected element.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$(selector).wrapInner(wrappingElement,function(index))
$(selector).wrapInner(wrappingElement,function(index))
$(selector).wrapInner(wrappingElement,function(index))

WrappingElement:

  • It is a compulsory parameter.
  • It is used to specify the HTML elements to be wrapped around the content of each selected element.
  • It can accept HTML elements, jQuery objects, and DOM elements as a value.

Function: It is an optional function to return the wrapping element.

Index: The index is passed as an argument of the function to specify the index position of the element in the set.

Example1:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!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").wrapInner("<em></em>");
});
});
</script>
</head>
<body>
<p>Hello!</p>
<p>Nice to see you.</p>
<button>Wrap Inner</button>
</body>
</html>
<!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").wrapInner("<em></em>"); }); }); </script> </head> <body> <p>Hello!</p> <p>Nice to see you.</p> <button>Wrap Inner</button> </body> </html>
<!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").wrapInner("<em></em>");
});
});
</script>
</head>
<body>
<p>Hello!</p>
<p>Nice to see you.</p>
<button>Wrap Inner</button>
</body>
</html>

Example2:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!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").wrapInner("<div></div>");
});
});
</script>
<style>
div{background-color: turquoise;width: 120px;
height: 20px;
margin: 5px;padding:2px; border:2px solid red;
}
</style>
</head>
<body>
<p>Hello</p>
<p>Guys!</p>
<button>Wrap Inner</button>
</body>
</html>
<!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").wrapInner("<div></div>"); }); }); </script> <style> div{background-color: turquoise;width: 120px; height: 20px; margin: 5px;padding:2px; border:2px solid red; } </style> </head> <body> <p>Hello</p> <p>Guys!</p> <button>Wrap Inner</button> </body> </html>
<!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").wrapInner("<div></div>");
});
});
</script>
<style>
div{background-color: turquoise;width: 120px;
height: 20px;
margin: 5px;padding:2px; border:2px solid red;
}
</style>
</head>
<body>
<p>Hello</p>
<p>Guys!</p>
<button>Wrap Inner</button>
</body>
</html>