Documentation

Release:jQuery 1.2/Manipulation

From jQuery JavaScript Library

Jump to: navigation, search

« Back to the full jQuery 1.2 Release Notes.

.wrapInner() / .wrapAll()

.wrapInner() is designed to wrap the inner child contents of each matched element (including text nodes).

Selects all paragraphs and wraps a bold tag around each of its contents.

$("p").wrapInner("<b></b>");

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    $("p").wrapInner("<b></b>");
  });
  </script>
  
</head>
<body>
  <p>Hello</p><p>cruel</p><p>World</p>
</body>
</html>

.wrapAll() will wrap all the elements in the matched set into a single wrapper element (this is different from .wrap() where each element in the matched set would get wrapped with an element).

Wrap a new div around all of the paragraphs.

$("p").wrapAll("<div></div>");

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    $("p").wrapAll("<div></div>");
  });
  </script>
  <style>div{ border: 2px solid black; }</style>
</head>
<body>
  <p>Hello</p><p>cruel</p><p>World</p>
</body>
</html>

.replaceWith() / .replaceAll()

replaceWith replaces all matched elements with the specified HTML or DOM elements.

Replace all the paragraphs with bold words.

$("p").replaceWith("<b>Paragraph. </b>");

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    $("p").replaceWith("<b>Paragraph. </b>");
  });
  </script>
  
</head>
<body>
  <p>Hello</p><p>cruel</p><p>World</p>
</body>
</html>

replaceAll replaces the elements matched by the specified selector with the matched elements.

Replace all the paragraphs with bold words.

$("<b>Paragraph. </b>").replaceAll("p");

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    $("<b>Paragraph. </b>").replaceAll("p");
  });
  </script>
  
</head>
<body>
  <p>Hello</p><p>cruel</p><p>World</p>
</body>
</html>

Cloning with Events

The clone method can now take an optional argument where, if true, it will also clone all the event handlers on all the cloned elements.

Create a button that's able to clone itself - and have the clones themselves be clonable.

$("button").click(function(){
  $(this).clone(true).insertAfter(this);
});

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    $("button").click(function(){
  $(this).clone(true).insertAfter(this);
});
  });
  </script>
  
</head>
<body>
  <button>Clone Me!</button>
</body>
</html>