Javatpoint Logo
Javatpoint Logo

jQuery replaceAll() method

The 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.

Syntax

The commonly used syntax of the replaceAll() method is given as follows.

Parameter values

content: 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.

Example1

In 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 Now

Output

After the execution of the above code, the output will be -

jQuery replaceAll() method

After clicking the given button, the output will be -

jQuery replaceAll() method

Example2

In 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 Now

Output

After the execution of the above code, the output will be -

jQuery replaceAll() method

After clicking the given button, the output will be -

jQuery replaceAll() method

Example3

In this example, we using the replaceAll() method to replace the first div element, which is the first child of the body element.

Test it Now

Output

After the execution of the above code, the output will be -

jQuery replaceAll() method

After clicking the given button, the output will be -

jQuery replaceAll() method
Next TopicjQuery on() method





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA