I would like to replace all occurence with a html contenteditable div. I need to replace the occurence with a tag to customize this with a css. But when I try to replace with a span, MutationObserver generate a infinite loop...
document.designMode = "on";
var text = document.getElementById('text');
let observer = new MutationObserver(mutations =>
mutations.forEach(mutation => {
console.log(text.textContent);
// let a = "a";
// a.style.color = "red";
text.textContent = text.textContent.replace('/', '<span>/</span>');
observer.disconnect();
const range = document.createRange();
const textNode = text.firstChild;
range.setStart(textNode, text.textContent.length);
range.setEnd(textNode, text.textContent.length);
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
// observer.disconnect();
})
);
observer.observe(text, {
childList: true,
characterData: true,
subtree: true,
});
<p id="text" contenteditable="true">I'm a text will be change</p>