Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: How to highlight text within an input box?

I'd like to know how to highlight some text within an input box; simple example, let's say I want to highlight all the terms starting by "@" while typing.

All the terms are retrieved from a remote autocomplete suggestions list.

The problem I'm facing is this input box, because I can't add html tags around the text and therefore can't make css on those specific terms...

Thanks for your help !

Cheers!

like image 337
jozi Avatar asked Aug 26 '12 10:08

jozi


People also ask

How do you highlight a text field?

To highlight an object like an icon in Windows, single-click the item. Once clicked, the icon or text should change to a different color, indicating it is highlighted. To highlight multiple icons or other objects, drag a box around all files you want to highlight.

How do you highlight text in Javascript?

The 'mark' tag If you surround any text inside of the mark tag, it will automatically get highlighted by the browser in this striking yellow color. That makes highlighting searched text quite a simple task then.


1 Answers

This, I hope, is what your after.

First take a look at highlightTextarea. Made by Julien L

With this you can do the following:

$("textarea").highlightTextarea({
    words: ["\\B@\\w+"],
    color: "lightblue",
    id: "mark"
});

The regex \\B@\\w+ assert position at an non word boundary (\B), followed by a @ and matches everything that falls under \w.

DEMO

like image 150
sQVe Avatar answered Sep 20 '22 17:09

sQVe