The replace
method replaces the specified HTML/XML element with the given HTML/XML element. This method is provided by the Mojo::DOM
module, which is an HTML/XML DOM parser with CSS selectors.
$dom->at('old html element')->replace('new html element')
This method will replace the given node and return a parent
or root
element.
Let’s look at an example of this.
use 5.010;use Mojo::DOM;# Parse the htmlmy $dom = Mojo::DOM->new('<div>Inside div <p id="a">Inside paragraph </p></div>');# replace node psay $dom->at('p')->replace('<h1>This is a new element</h1>');
In the code snippet above:
Mojo::DOM
module.$dom
scalar.p
with the h1
element using the replace
method and print the returned parent or root element.