I am trying to replace some text "on the fly", surrounding it with a span.
Example:
A person prints when they type the word "back" and his space after it, he makes the whole sentence or line with this word in it bold.
Right now I have a CSS class setting for it, and I'm trying to include it in a range, but I'm not sure if this works ... it also resets something new at any time, so the cursor goes back to the beginning, and that doesn't allow me move forward.
Here is what I still have (my regex is wrong too):
function replace(e) {
if (e.keyCode == 13) {
element = document.getElementById("script");
rg = /^INT/;
element.innerHTML = element.innerHTML.replace(
rg,
'<span id="bold">$1</span>'
);
};
};
<div contenteditable="true" id="script" style="width: 100%; height: 500px; border: 1px solid black;" onkeydown="replace(event)">
Replace
</div>
Run code
source
share