jQuery replaceAll() replace last Paragraph

Introduction

Replace every p element that is the last child of its parent

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>/*  ww  w  .ja  v a 2  s  .  c o m*/
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("<span><b>Hello world!</b></span>").replaceAll("p:last-child");
  });
});
</script>
</head>
<body>

<button>test</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>



PreviousNext

Related