0

Actually I am making a online code editor. I need to change color of words like "void" , "digitalWrite" etc when user types in my contenteditable div. For example if anyone types void then color of only void should become red How can I do these. Here is html

<div id="mydiv" contenteditable></div>

1 Answers1

0


try it by adding an input event listener to the div and if there is a keyword like void, then you could surround it with a span with a specific color.
Pls don't do this!!! This is a very big vulnerability because anyone can code for you.
But if you really need this, you can use this code:

 document.querySelector(“.editor”).addEventListener(“input”, () => {
   var content = document.querySelector(“.editor”).innerHTML;
   var newContent = content.substr(0, content.search("void")) + "<span>void" + "</span>" + content.substr(content.search("void") + "void".length, content.length);
 }, false);

Sry if its not working, I hate coding on phone...

ShadowLp174
  • 502
  • 2
  • 9