jQuery replaceAll() methodThe replaceAll() method in jQuery replaces the selected elements with new HTML elements. This method replaces the elements and their contents with other HTML elements. It returns the selected elements with new content. SyntaxThe commonly used syntax of the replaceAll() method is given as follows. Parameter valuescontent: It is the mandatory parameter that specifies the content to insert. It must be HTML tags. selector: It is also the mandatory parameter. It specifies the content required to be replaced. It can be the tag name, id or class name. Let's see some examples to understand the use of the replaceAll() method. Example1In this example, there are two div elements, a paragraph element, and a button on which we are applying the replaceAll() method. After clicking the button, div element with id = "d1" gets replaced with the heading h1, and the div element with id = "d2" gets replaced with the heading h2. The text of paragraph element with id = "p1" gets replaced with the new content, and the button with id = "btn" gets replaced with a new paragraph element with some content. Test it NowOutput After the execution of the above code, the output will be - After clicking the given button, the output will be - Example2In this example, there are some paragraph elements and a button. We are replacing all given paragraphs and their contents with the heading h2 and its content. We have to click the given button to replace the paragraphs with heading. Test it NowOutput After the execution of the above code, the output will be - After clicking the given button, the output will be - Example3In this example, we using the replaceAll() method to replace the first div element, which is the first child of the body element. Test it NowOutput After the execution of the above code, the output will be - After clicking the given button, the output will be - Next TopicjQuery on() method |