jQuery wrap() with callback function

Introduction

Wrap a div element around the 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>/*www . j  a  v  a  2s.  c o  m*/
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").wrap(function(){
        return "<div></div>"
    });
  });
});
</script>
<style>
div {
  background-color: yellow;
  padding: 10px;
}
</style>
</head>
<body>

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

<button>test</button>

</body>
</html>



PreviousNext

Related