← Back to Index

.replaceWith()

Replace each element in the set of matched elements with the provided new content.

Examples

1. Replace with HTML

// <h3>Old Header</h3>
$('h3').replaceWith('<h2>New Header</h2>');

Result

<h2>New Header</h2>

2. Replace with Element

const $div = $.t('<div>', { class: 'new' });
$('.old').replaceWith($div);

Result

<div class="new"></div>

Parameters

Parameter Type Description
newContent string | element | SR HTML string, DOM element, or SR object to replace each matched element.

Returns: The original SR object (containing the removed elements) for chaining.