replaceAll() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:replaceAll

Description

The replaceAll() method replaces selected elements with new HTML elements.

Syntax

Parameter RequireDescription
content Required. content to insert (must contain HTML tags)
selector Required. which elements to be replaced

The following code shows how to Replace every p element that is the last child of its parent, with a bold span element.

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("<span><b>Hello world!</b></span>").replaceAll("p:last-child");
    });/*from   www.j  av a2  s  .  c o m*/
});
</script>
</head>
<body>

<div style="border:1px solid">
  <p>The first paragraph in div.</p>
  <p>The last paragraph in the div.</p>
</div><br>

<div style="border:1px solid">
  <p>The first paragraph in another div.</p>
  <p>The last paragraph in another div.</p>
</div>

<button></button><br><br>

<div style="border:1px solid">
  <p>The first paragraph in div.</p>
  <p>The last paragraph in the div.</p>
</div><br>

<div style="border:1px solid">
  <p>The first paragraph in another div.</p>
  <p>The last paragraph in another div.</p>
</div>

</body>
</html>

Related Tutorials