Reputation:
Is there any way to highlight text in textarea
without backend code?
Upvotes: 5
Views: 11549
Reputation: 643
For anyone who ends up here with crossed wires in their head thinking of highlighting in the sense of selecting text (which is how I ended up here), there are 2 methods available.
HTMLInputElement: setSelectionRange()
https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setSelectionRange
Select a subset of the text within a textarea using carat position start and end.
HTMLInputElement: select()
https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/select
Select all the text within a textarea.
To highlight text as in set a particular background colour, i.e. in the sense of using a highlighter pen on paper, a combination of css and js will be needed as per the other answer.
Upvotes: 0
Reputation: 136
You can't actually highlight text in a <textarea>
. Any markup you would add to do so would simply display as plain text within the <textarea>
. But you can carefully craft some CSS and Javascript code to fake it:
Please check out this article Highlight Text Inside a Textarea for more information on how it can be crafted.
Upvotes: 7