Given HTML as a string, Xpath, and offsets. I need to highlight this word.
In the following case, I need to highlight Child 1
HTML text:
<html>
<body>
<h2>Children</h2>Joe has three kids:<br/>
<ul>
<li>
<a href="#">Child 1 name</a>
</li>
<li>kid2</li>
<li>kid3</li>
</ul>
</body>
</html>
XPATH as: /html/body/ul/li[1]/a[1]
Offsets: 0,7
Render - I use react
in my application. The following shows what I have done so far.
public render(){
let htmlText =
let doc = new DOMParser().parseFromString(htmlRender,'text/html');
let ele = doc.evaluate("/html/body/ul/li[1]/a[1]", doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
let spanNode = document.createElement("span");
spanNode.className = "highlight";
spanNode.appendChild(ele);
return(
<h5> Display html data </h5>
<div dangerouslySetInnerHTML={{__html: htmlText}} />
)
I want to avoid using jquery. Want to do it in Javascript (if there is a risk)!
Edit:
So, if you notice a function Render
, it uses dangerouslySetHTML
. My problem is that I cannot manipulate the line that is displayed.
source
share