jQuery | replaceAll() with Examples

The replaceAll() method is an inbuilt method in jQuery which is used to replace selected elements with new HTML elements.

Syntax:

$(content).replaceAll(selector)

Parameters: This method accepts two parameter as mentioned above and described below:

  • content: It is the required parameter which is used to specify the content to insert.
  • selector: It is required parameter which specify the elements to be replaced.

Return Value: This method return the selected elements with new content.

Below program illustrates the above function:

Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>The replaceAll Method</title>
        <script src=
        </script>
          
        <!-- jQuery code to show the working of this method -->
        <script>
            $(document).ready(function() {
                $("button").click(function() {
                    $("<h1>GeeksforGeeks!</h1>").replaceAll("p");
                    $("h1").css({"color":"green"});
                });
            });
        </script>
        <style>
            div {
                width: 60%;
                height: 150px;
                padding: 10px;
                border: 2px solid green;
                font-size: 20px;
                text-align:center;
            }
            p {
                font-size:25px;
                font-weight:bold;
            }
        </style>
    </head>
    <body>
        <div>
            <p>Welcome to </p>
              
            <!-- click on this button and see the change -->
            <button>Click Here!</button>
            <br>
        </div>
    </body>
</html>

chevron_right


Output:
replaceall method



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.