12

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

1
  • 1
    do you mean anything wrapped in a <code> tag?
    – bimbom22
    Jul 21, 2010 at 16:45

5 Answers 5

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...

1
  • 4
    I know this is really late but I dont get HOW to use codemirror
    – user115422
    Aug 23, 2012 at 22:03
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.

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

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.

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.