12

Is there any good and logical ways to highlight code inside textarea via jQuery? If there is, please share.

Martti Laine
  • 12,655
  • 22
  • 68
  • 102

5 Answers5

22

You could use CodeMirror http://codemirror.net/

...a versatile text editor implemented in JavaScript for the browser. It is specialized for editing code, and comes with a number of language modes and addons that implement more advanced editing functionality.

A rich programming API and a CSS theming system are available for customizing CodeMirror to fit your application, and extending it with new functionality...

gnat
  • 6,213
  • 108
  • 53
  • 73
garpo
  • 221
  • 2
  • 3
4

No, there isn't. You can apply styles to the entire textarea, but not part of the text in it.

You would need something that works as a replacement for the textarea, here is a list of some.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

You can highlight a part of a text with the follow search code:

<script type="text/javascript">
    $(document).ready(function() {
        $("textarea").highlightTextarea({
              words: ["first word","an other word"]
        });
    });
</script>

Find more about highlighting in a textarea on Highlight text into a textarea

Raymond van Os
  • 191
  • 1
  • 5
  • 18
1

If by code, you mean SQL, PHP etc., take a look at Codepress. It's a real-time, syntax highlighting editor, written in Javascript.

Edit:

If you'd prefer a more modern and actively-maintained alternative, you should look at Ace.

elo80ka
  • 14,837
  • 3
  • 36
  • 43
0

i was looking too for a live syntax coloring best solution so i found a working , ancient software(2007) that is older than textarea(witch is working too) :

1.Codepress

https://sourceforge.net/projects/codepress/files/

then in index.html the line 117 replaced with

<textarea spellcheck="false" id="codepress2" class="codepress javascript linenumbers-off" style="width:700px;height:200px;" wrap="off">

will had live syntax coloring like notepad++ / editplus in web meaning small code could be "repainted" , "reforget" to this age ...

it's up to you ...

2.MIT LDT =minimalist syntax coloring
https://github.com/kueblc/LDT

..all tests i did are in a custom apache php 8.1 mariadb custom server (remote buggy/spies free) so these two works fine!

so the answer is : in case you open to recoding something these can be recoded in jquery with the author accept (check the licenses for more info)