How can I make a syntax highlighting contenteditable div element with js??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 16

How can I make a syntax highlighting contenteditable div element with js???

like < ,>,and other special characters should be displayed in different colours

2nd Aug 2020, 4:04 PM
RISHABH
RISHABH - avatar
16 Answers
2nd Aug 2020, 4:08 PM
Gordon
Gordon - avatar
2nd Aug 2020, 4:10 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 8
David Carroll he has unlimited amount of time :D
3rd Aug 2020, 8:25 PM
r8w9
r8w9 - avatar
+ 6
lionel sisso syntax highlighting in a textarea using js
2nd Aug 2020, 4:05 PM
PresidentOfYes12
PresidentOfYes12 - avatar
+ 6
Rishabhใƒ„ Using JavaScript you can do. Refer this w3school link https://www.w3schools.com/howto/howto_syntax_highlight.asp
2nd Aug 2020, 4:17 PM
AอขJ
AอขJ - avatar
+ 6
You can't do that with a textarea but you'd likely be happy with an alternative. I used CodeMirror in a previous project and it worked well: https://codemirror.net/ CodeMirror creates a syntax-highlighted editor but through lots of non-textarea elements like spans.
2nd Aug 2020, 4:19 PM
Josh Greig
Josh Greig - avatar
+ 5
Rishabhใƒ„ The answer from Josh Greig appears to be a solid solution if you are flexible to plug in a new editor. That library appears to support themes and autocompletion capabilities as well.
2nd Aug 2020, 5:23 PM
David Carroll
David Carroll - avatar
3rd Aug 2020, 1:50 PM
Calviีฒ
Calviีฒ - avatar
+ 4
AJ #L1G3 Sir I want a syntax highlighting system which works when the text is being written inside the contenteditable div element
2nd Aug 2020, 5:00 PM
RISHABH
RISHABH - avatar
+ 3
Use regex in js to find special character in an oninput function so that it check all character as they entered.it will return true if special character found. Then use a condition If special character found (target the last character of div).style.color ="youfavcolor";
2nd Aug 2020, 4:14 PM
Divya Mohan
Divya Mohan - avatar
+ 3
Calviีฒ You're pretty amazing for taking a moment to demo this. ๐Ÿ˜‰๐Ÿ‘Œ
3rd Aug 2020, 8:21 PM
David Carroll
David Carroll - avatar
+ 2
I don't understand ..๐Ÿค”๐Ÿค”
2nd Aug 2020, 4:05 PM
LIONEL
LIONEL - avatar
+ 2
can you explain your problem better..๐Ÿค”
2nd Aug 2020, 4:05 PM
LIONEL
LIONEL - avatar
+ 2
I check AJ #L1G3 suggestion They have use the same theme I suggested. I suggest you to try yourself and visit there if any problem or professional modification.
2nd Aug 2020, 4:25 PM
Divya Mohan
Divya Mohan - avatar
+ 1
Expliquez plus
3rd Aug 2020, 11:25 AM
Yomoussa COULIBALY
Yomoussa COULIBALY  - avatar
+ 1
<script type="text/javascript"> // jQuery $(document).ready(function() { $('#area').focus(function() { .. } // focus I would imagine I need. } </script> <div id="area" contentEditable="true"></div>
3rd Aug 2020, 12:15 PM
Yomoussa COULIBALY
Yomoussa COULIBALY  - avatar