.replaceWith()

Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed.

.replaceWith(newContent)🡢 jQuery

newContent htmlString, Element, Array, jQueryThe content to insert. May be an HTML string, DOM element, array of DOM elements, or jQuery object.

.replaceWith(function())🡢 jQuery

function() FunctionA function that returns content with which to replace the set of matched elements.

The .replaceWith() method removes content from the DOM and inserts new content in its place with a single call. 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>
</div>

The second inner <div> could be replaced with the specified HTML:

$("div.second").replaceWith("<h2>New heading</h2>");

This results in the structure:

<div class="container">
  <div class="inner first">Hello</div>
  <h2>New heading</h2>
  <div class="inner third">Goodbye</div>
</div>

All inner <div> elements could be targeted at once:

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

This causes all of them to be replaced:

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

An element could also be selected as the replacement:

$("div.third").replaceWith($(".first"));

This results in the DOM structure:

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

This example demonstrates that the selected element replaces the target by being moved from its old location, not by being cloned.

The .replaceWith() method, like most jQuery methods, returns the jQuery object so that other methods can be chained onto it. However, it must be noted that the original jQuery object is returned. This object refers to the element that has been removed from the DOM, not the new element that has replaced it.

On click, replace the button with a div containing the same word.

JS
<button>First</button>
<button>Second</button>
<button>Third</button>
CSS
button {
  display: block;
  margin: 3px;
  color: red;
  width: 200px;
}
div {
  color: red;
  border: 2px solid blue;
  width: 200px;
  margin: 3px;
  text-align: center;
}
HTML
$("button").click(function () {
  $(this).replaceWith("<div>" + $(this).text() + "</div>");
});
DEMO

Replace all paragraphs with bold words.

JS
<p>Hello</p>
<p>cruel</p>
<p>World</p>
HTML
$("p").replaceWith("<b>Paragraph. </b>");
DEMO

On click, replace each paragraph with a div that is already in the DOM and selected with the $() function. Notice it doesn't clone the object but rather moves it to replace the paragraph.

JS
<p>Hello</p>
<p>cruel</p>
<p>World</p>
<div>Replaced!</div>
CSS
div {
  border: 2px solid blue;
  color: red;
  margin: 3px;
}
p {
  border: 2px solid red;
  color: blue;
  margin: 3px;
  cursor: pointer;
}
HTML
$("p").click(function () {
  $(this).replaceWith($("div"));
});
DEMO

On button click, replace the containing div with its child divs and append the class name of the selected element to the paragraph.

JS
<p>
  <button>Replace!</button>
</p>
<div class="container">
  <div class="inner">Scooby</div>
  <div class="inner">Dooby</div>
  <div class="inner">Doo</div>
</div>
CSS
.container {
  background-color: #991;
}
.inner {
  color: #911;
}
HTML
$("button").on("click", function () {
  var $container = $("div.container").replaceWith(function () {
    return $(this).contents();
  });

  $("p").append($container.attr("class"));
});
DEMO

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)