The jQuery replaceAll() method can be used to replace all the selected elements with the specified HTML tags. It replaces all elements as well as its content with the other HTML element.
Syntax
The syntax of this method is given below:-
Description of the Parameters
The description of these parameters is given below.
Parameter Name | Description |
---|---|
selector | Specify the element to select and replace all the elements. You can use the element tag name, class name, or id to select. It is a required field. |
content | You have to specify the HTML element to replace it with the selected element. It is an HTML tag or element that can contain some text content. It is a required field. |
jQuery replaceAll() Method to Replace All Selected Element
If you want to replace the element from the specified element, you have to use the method as given below. There are three paragraph elements, you have to replace all of them with em element.
Example
1 2 3 4 5 6 7 8 9 10 11 |
<script> $(document).ready(function(){ $('.mybtn').click(function(){ $("<em>Welcome to TutorialDeep!</em>").replaceAll("p"); }); }); </script> <p>First paragraph.</p><br> <p>Second paragraph.</p><br> <p>Third paragraph.</p><br> <button type="button" class="mybtn">Click to Replace</button> |
Output
First paragraph.
Second paragraph.
Third paragraph.
When you click the button given above, it replaces the paragraph element with the em elements. After the replacement, the output contains the three em element.
You may also like to read
I hope you like this tutorial on jQuery replaceAll() method. If you have any queries regarding the tutorial, please comment below.
References