1

I have an element like below;

<p> I am lost </p>`

I need to highlight "am" like below;

<p> I <mark style="background-color:#FFFF00;">am</mark> lost </p> 

My code is like this.

String newText = "I <mark style=\"background-color:#FFFF00;\">am</mark> lost"; 
element.text(newText);

But when I print the element it looks like this.

<p>I &lt;mark style="background-color:#FF0000;"&gt;am&lt;/mark&gt; lost</p>

Are there any ways to force "<" and ">" characters to elements using Jsoup?

Eranga Heshan
  • 5,133
  • 4
  • 25
  • 48
  • I don't know if this helps, but I had simular problem and solve it using `var = "<‍div>I am lost"`. `‍` is the Unicode Character to [Zero Width Joiner](https://en.wikipedia.org/wiki/Zero-width_joiner) – Matheus Cuba Oct 27 '17 at 19:40
  • @MatheusCuba Thank you. But it did not work :-) – Eranga Heshan Oct 27 '17 at 19:43
  • I think this is a duplicate of [How to prevent jsoup converting special characters?](https://stackoverflow.com/questions/41186195/how-to-prevent-jsoup-converting-special-characters) which is a duplicate of [Jsoup is escaping content of iframe](https://stackoverflow.com/a/41162438/42962). – hooknc Oct 27 '17 at 20:40

1 Answers1

2

Instead of element.text(newText) you should use element.html(newText)

Eranga Heshan
  • 5,133
  • 4
  • 25
  • 48