jQuery replaceAll()

Introduction

Replace all <p> elements with <h2> elements:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>//from  w w  w. jav  a2  s  . c  o  m
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("<h2>Hello world!</h2>").replaceAll("p");
  });
});
</script>
</head>
<body>

<button>Replace all p elements with h2 elements</button><br>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<p>This is another paragraph.</p>

</body>
</html>

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

$(content).replaceAll(selector)
Parameter Optional Description
content Required. the content to insert
selector Required. elements to be replaced



PreviousNext

Related