I have a string
const string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent tristique elit in volutpat iaculis. Proin a tincidunt turpis, et condimentum libero. Duis convallis nulla eu mattis porta. Nulla facilisi. Proin nec viverra orci. Nunc aliquam enim orci, ut dictum ipsum auctor ut. Quisque consectetur vestibulum tortor, mollis hendrerit velit hendrerit vel. In hac habitasse platea dictumst. Morbi volutpat lectus purus, eu sagittis odio viverra in. Phasellus vel volutpat felis. Proin a metus sit amet ipsum congue faucibus nec faucibus nisl. Aenean eu nisl ac velit dapibus vulputate non efficitur urna. Phasellus laoreet suscipit dictum. Curabitur sit amet justo at nisi dictum dapibus."
I want to be able to highlight some coherent sequence of words and show a tooltip when hovering the words.
What would the data structure look for this?
I guess it should be something like
id, wordIndexStart, wordIndexEnd, note
=======================================
1, 0, 5, Some note
in order to highlight the first 6 words, giving me something like
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent tristique elit in volutpat iaculis (...)
but how do I return this string with this highlighted text in React or something similar?
Normally, I would print my text with
<p>{string}</p>
but I guess it should be something like
<p>{string.split(" ").map(word => <span>{word}).join(" ")</p>
but how do I create a <span>
around, for instance, the 6 first words?
Highlight Text Using the Mark Tag Method in JavaScript Another method that you can use to highlight the text is the mark tag. If you surround any text inside the mark tag, the browser will automatically highlight it in a striking yellow color. This will make highlighting a searched text quite a simple task then.
First thing first when you are entering some string on the search box and press the search button then a simple JavaScript function will call named as highlight() which has the main role is to highlight the search text that you had entered in the search box.
How about this:
const rules = {
start: 0,
end: 5,
tooltip: "Some note"
};
const Highlight = ({ words }) => {
const portion = words.split(" ").slice(rules.start, rules.end).join(" ");
const rest = words.split(" ").slice(rules.end).join(" ");
return (
<span>
<span className="highlight">{portion}</span>
<span className="standard">{rest}</span>
</span>
);
};
const App = () => {
const sentence = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent tristique elit in volutpat iaculis.';
return (
<div>
<Highlight words={sentence} />
</div>
)}
;
ReactDOM.render(<App />, document.getElementById("root"));
.highlight {
font-weight: bold;
color: red;
}
.standard {
color: grey;
}
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With