.replaceAll( target )Returns: jQuery

Description: Replace each target element with the set of matched elements.

The .replaceAll() method is similar to .replaceWith(), but with the source and target reversed. Consider this DOM structure:

<div class="container">
  <div class="inner first">Hello</div>
  <div class="inner second">And</div>
  <div class="inner third">Goodbye</div>

We can create an element, then replace other elements with it:

$( "<h2>New heading</h2>" ).replaceAll( ".inner" );

This causes all of them to be replaced:

<div class="container">
  <h2>New heading</h2>
  <h2>New heading</h2>
  <h2>New heading</h2>

Or, we could select an element to use as the replacement:

$( ".first" ).replaceAll( ".third" );

This results in the DOM structure:

<div class="container">
  <div class="inner second">And</div>
  <div class="inner first">Hello</div>

From this example, we can see that the selected element replaces the target by being moved from its old location, not by being cloned.

Additional Notes:

  • The .replaceAll() method removes all data and event handlers associated with the removed nodes.


Replace all the paragraphs with bold words.

<!doctype html>
<html lang="en">
  <meta charset="utf-8">
  <title>replaceAll demo</title>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
$( "<b>Paragraph. </b>" ).replaceAll( "p" );


© The jQuery Foundation and other contributors
Licensed under the MIT License.