My problem is very specific, im trying to create online code editor with colored scripts.
Im using contenteditable
div
so i can add span
. When im typing the word script
the caret
is going at the begining of the field, and each time i try add more text the caret again is going at the begining.
If someone helps me to fix this it will help to many people who is trying to do that.
Here's my code:
<html>
<body>
<div contentEditable style="width: 400px; height: 200px; border: 1px solid red;"></div>
<script>
document.querySelector("div").addEventListener("input", function() {
document.querySelector("div").innerHTML= document.querySelector("div").innerHTML
.replace("script","<span style='color:red;'>script</span>");
}, false);
</script>
</body>
</html>