.replaceAll()
methodThe .replaceAll(target)
method in jQuery is used to replace the target
element with the matched elements from the DOM.
.replaceAll()
method Syntax.replaceAll(target)
target
is a jQuery object, DOM element(s), or selector string indicating which element(s) to replace.
.replaceAll()
method ExampleCreate an element and replace paragraphs with it.
HTML
<div class="container"> <p>Learn JavaScript</p> <p>Learn jQuery</p> Lautturi tutorial </div>
jQuery
$('<b>Learning online</b>').replaceAll('p');
Result
<div class="container"> <b>Learning online</b> <b>Learning online</b> Lautturi tutorial </div>
Select an element and replace paragraphs with it.
Please note that the selected element is moved from its old location but not cloned.
HTML
<div class="container"> <p class="js">Learn JavaScript</p> <p class="cpp">Learn C++</p> <p class="jquery">Learn jQuery</p> </div>
jQuery
$('.jquery').replaceAll('.js');
Result
<div class="container"> <p class="jquery">Learn jQuery</p> <p class="cpp">Learn C++</p> </div>