I have a form in which there are input elements of type text that may contain text starting with @, now what I want to achieve is to highlight the partial text inside the input elements that starts with @. I have seen other solutions like using div with contenteditable and other solutions like using editor that does work but they changes the actual content with the HTML and this is not what I don’t want to use.
Now I have noticed chrome implements find and highlight the text as shown in below image and that seems I can modifify its logic with my requirements,
Is there any hints or solutions ? Please help me with this case.
2
Answers
You could do something like the following:
The idea is that you keep track of the input-value and once you are done editing, you layer the rendered text on top of the input. When you are actually editing, you just display the input itself.
With
pointer-events: none
we can prevent the span from catching events so all events are received by the input.You cannot style substrings of a true text field. If you want to keep the input for functionality reasons but show the user a styled field, you can have a
contenteditable
on top with a hidden input on the bottom and bind them so that the raw text gets submitted in the input but your contenteditable is just for show.Example case: