I'm trying to reproduce a behavior of syntax Highlight.
How need to work ?
When I write they need to review the text to find some especific words and put a color.
So, what I already try:
HTML:
<div id="text" contenteditable="true">O </ div>
JS:
var text = document.getElementById('text');
text.onkeyup = function(evt) {
if (this.textContent.indexOf("Brasil") > 0) {
text.innerHTML = this.textContent.replace("Brasil", "<b class='green'>Brasil<b>");
}
};
CSS:
.green{
color:green;
}
Run on jsfiddle
The problems:
1 - The cursor moves to start.
2 - After write one word using the indexOf they will always replace with the same data.