Change style of a specific text inside a input text

Viewed 56

I want to apply some style to a specific text inside an input text tag

If the user introduces "Hello world" in the input text tag and the special text is "world"

I hope this Output:

enter image description here

My html code looks like this

<input name="inputTest" ng-model="inputTest">

My javascript code is activated by an external button that call a "checkInput" function

let specialText = "world"
textArr = inputTest.split(/(\s+)/);
for(const text of textArr){
   if(text == specialText){
      //Do something to change style of the "special text"
   }
}
3 Answers

One rough approach you can try like:

In your class file:

inputText: string;
specialText: string;

onTextInput(text: string): void {
   const inputValues = text.split(' ');
   this.inputText = inputValues[0]; // Hello
   this.specialText = inputValues[1]; // World
}

In the template file:

<input type="text" #specialInput (keydown)="onTextInput(specialInput.value)" />
<p>{{inputText}} <strong>{{specialText}}</strong></p>
Related