Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a string to a text area with different colours?

I am trying to work out how to write a function where I pass in a string and this string is added to a textarea. This I can do, but I want to parse that string and change the colour on different parts of the string.

In other words, find the words "select", "where" and turn them blue. Then find the words "AND", "OR", "<" and turn them grey and anything in between two single quotes to be the colour red.

I am hoping for a simple function rather than using syntax highlighting libraries, I am not even sure they will work with strings that are dynamically generated.

How can I do this? I am able to make use of JQuery if this makes things easier?

Thanks all

like image 317
Abs Avatar asked Dec 15 '25 20:12

Abs


1 Answers

text inside <textarea> elements is plain text only, meaning that it cannot be styled. what you can do is create a div with contenteditable and work with that, check this link out for a reference: http://www.west-wind.com/Weblog/posts/778165.aspx

like image 195
bimbom22 Avatar answered Dec 17 '25 11:12

bimbom22



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!