Reputation: 370
I want to replace all image tags with div
tag. I am able to select all tags and I know that I have to use replaceWith
. But I am unable to use it.
And if I use TextNode
for replacing it with <div> </div>
and it converts into &lt;div&gt; my div &lt;/div&gt;
I know &lt;
and &gt;
is for <
and >
Please help me.
Upvotes: 4
Views: 14189
Reputation:
just use find method and pass specific tags
let tag = soup.find('tag_name') tag.name = 'new_tag';//replaced tag
for multiple tag just put inside loop
let tag = soup.findAll('tag_name'); if (tag) { for (let i = 0; i < tag.length; i++) { tag[i].name = 'your_new_tag'; } }
Upvotes: 0
Reputation: 31
This is the simplest way of achieving this:
Elements elements= doc.getElementsByTag("img");
elements.tagName("div");
Hope that is working for you.
Upvotes: 3
Reputation: 11607
I guess you are replacing with element.replaceWith(new TextNode("<div></div>"),"");
?
A Textnode
is for text and escapes content - thats why you see the HTML entities. You need to replace with a tag, so do something like element.replaceWith(new Element(Tag.valueOf("div"), ""));
.
Upvotes: 13