replaceWith()

Replaces one HTML element or each element in a set of HTML elements.

replaceWith(content)

Replaces one HTML element or each element in a set of HTML elements. Returns the result set.

content

A query result. This can be an HTML string or a result set.

Examples

Replace elements with a snippet

The following script loads a snippet and then replaces the elements matched by the script's selector with the snippet.

Copy
var snippet = loadhtml('snippets/mysnippet.html'); 
results.replaceWith(snippet);

Replace elements with a set of snippets

The following script loads snippets and adds their elements to a new, empty result set (using query()). Then it replaces a placeholder in the template with the set of snippets.

Copy
var chapters = query(); 
for ( var i = 1; i <= 4; i++) { 
chapters = chapters.add(loadhtml('snippets/Chapter' + i + '.html'));

results.replaceWith(chapters);