We are applying the replaceWith() method on all div elements and on the paragraph elements with class = "para". In this example, there are some paragraph elements and some div elements.
![javascript replacewith javascript replacewith](https://www.codegrepper.com/codeimages/replace-function-javascript.png)
![javascript replacewith javascript replacewith](https://esr-energy.ru/800/600/https/stroimaterials.ru/wp-content/uploads/2018/10/Prohodnoy-vyiklyuchatel-58.jpg)
Now, in the next example, we will use the function(index) parameter of the replaceWith() method. $("#p1").replaceWith(" This is a paragraph element ") Īfter the execution of the above code, the output will be -Īfter clicking the given button, the output will be. $("#d2").replaceWith(" Welcome to the ") Click the following button to see the effect This is an example of using the jQuery replaceWith() method. 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 div element with id = "d3". Here we are using the newContent parameter of the replaceWith() method.Īfter clicking the button, the div element with id = "d1" gets replaced with the heading h1, and the div element with id = "d2" gets replaced with the heading h2. In this example, there are two div elements, a paragraph element, and a button on which we are applying the replaceWith() method.
Javascript replacewith how to#
Let's see some examples to understand how to use the replaceWith() method. The index argument is used to return the element's index position. It is the function that returns the content to replace.
![javascript replacewith javascript replacewith](https://cf3.ppt-online.org/files3/slide/c/cEukSqXofjNys7wTWdFp3AM2RmQhe0rnP15zvI/slide-10.jpg)
It can be HTML elements, DOM elements, or jQuery objects.įunction(index): It is an optional parameter. It is the content to replace the selected elements. The parameter values of the replaceWith() method are defined as follows. $(selector).replaceWith(newContent, function(index))