wrap() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:wrap

Description

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

Syntax

Parameter Require Description
wrappingElement Required.HTML elements/jQuery objects/DOM elements to wrap around each selected element
function(index) Optional.a function that returns the wrapping element
  • index - Returns the index position of the element in the set

The following code shows how to Wrap a <div> element around each <p> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p").wrapInner("<b></b>");
    });//  w ww  .jav  a2 s.  c  o m
});
</script>
</head>
<body>

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

<button>Wrap a b element around the content of each p element</button>

</body>
</html>

Related Tutorials