Modify the appearance of certain text within an input field

I need some help adding style to specific text within an input field.

For example, if the user types in "Hello world" and the special text is "world"

I want to achieve this result:

https://i.sstatic.net/Ozd6n.png

This is what my HTML code looks like:

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

My JavaScript function is triggered by an external button that calls the "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"
   }
}

Answer №1

It's uncertain whether this task can be accomplished using an input element. One alternative worth exploring is utilizing editable content. https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Editable_content Editable content allows for greater styling flexibility compared to a traditional input. You could also implement javascript alongside editable content to manage the styling.

Answer №2

If you're looking for a quick solution, you might want to consider this approach:

Code snippet for your component:

inputValue: string;
additionalValue: string;

onInputUpdate(value: string): void {
   const inputArray = value.split(' ');
   this.inputValue = inputArray[0]; // First
   this.additionalValue = inputArray[1]; // Second
}

Template file snippet:

<input type="text" #additionalInput (keydown)="onInputUpdate(additionalInput.value)" />
<p>{{inputValue}} <strong>{{additionalValue}}</strong></p>

Answer №3

One option is to incorporate syntax highlighting libraries such as Prism.js for a more visually appealing display.

By creating custom language definitions and integrating them for live input processing, users can enhance the interactive experience.

For instance, detailed instructions on defining languages in Prism.js can be found at:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

identifies a data point from a combined dropdown menu

Is there a way to detect a specific value from a multiplied combo box? For example, when the value in one of the combo boxes changes to "2", a component is displayed. However, if the value in another combo box changes to anything other than "2", the compon ...

Align content distributed evenly will not display a division

I am currently using React to code and learning by doing so. I have two different images displayed, each followed by a div element with a height of 20px and a brown background color. I have set the height to "100%" and justifyContent to "space-between", bu ...

What is the best way to adjust the border radius of a TextField component in Material-UI React?

I've been attempting to customize the border radius of a TextField in MUI without any success. The current borderRadius is set to 4px, and I'm trying to change it to 0px. Here is the code I've been using: <TextField id="time&q ...

Continuous Playback of Sound

Is there a way to autoplay an audio in loop without being blocked by browsers? Code : <audio id="audio1" src="assest/sound/1.mp3" autoplay="" /> <script> a = document.getElementById('audio1&apo ...

Utilizing JQuery for retrieving a filename

I have a unique file upload button on my website. To provide the user with visual feedback about their chosen file, I modify the html of a div to display the file name. My jquery code is as follows: $("input[type=file]").change(function() { var filen ...

Create a CSV file through an MVC Web API HttpResponse and retrieve it using AngularJS for downloading

I am attempting to create a CSV file from my web API and retrieve that file using angularjs. Below is an example of my API controller: [HttpPost] public HttpResponseMessage GenerateCSV(FieldParameters fieldParams) { var output = new byte[ ...

Blend Mode / Vue CLI / Remote server routing

I'm looking for a solution to set up a proxy in an AngularCLI/Webpack environment. The main goal is to forward requests from http://localhost:4200/rest to https://someserver.com/somepath/rest. One challenge is that the endpoint is using HTTPS instead ...

Inquirer doesn't waste time lingering for user input following a prompt

After initiating the prompt, I'm encountering an issue where inquirer doesn't pause for user input and instead immediately advances to the next command line. Below is the code I'm using: import inquirer from 'inquirer'; var link; ...

Error: The process.binding feature is not supported in the current environment (browserify + selenium-webdriver)

Recently, I've been attempting to execute a Node.js code on the client side of my browser. To make my code compatible with browsers, I am using Browserify for conversion purposes. Below is the command I use for this transformation: browserify te ...

What could be causing the browser to only partially display an image?

Have you encountered any issues with the browser not displaying an image fully? It seems like there may be a problem with the image download or rendering process, causing only part of the image to load. My application utilizes node and socket.io. Below ar ...

What is the importance of having a reference path for compiling an AngularJS 2 project using gulp-typescript?

I wanted to modify the Angular Tour Of Heros project to utilize gulp from this Github Repository. This is the gulpfile.json file I came up with: const gulp = require('gulp'); const del = require('del'); const typescript = require(&apo ...

Streamline retrieval of alphanumeric indexing within json

When accessing json data using jquery, I came across an index structure like this: data.rows[0].student_1 data.rows[0].student_2 data.rows[0].student_3 and so on... Now, I'm looking to automate this process by creating a loop that allows me to acces ...

What is causing my AJAX Contact Form to navigate away from the original page?

I configured a contact form on my website more than 5 years ago, and I vividly remember that it used to show error/success messages directly on the page within the #form-messages div. However, recently, instead of staying on the contact form page and displ ...

Creating the appearance of a white sheet of paper on a computer screen

I appreciate all the comments provided earlier. Looking back, I realize I should have been more thorough in my explanation. Hopefully, the updated version will make things clearer. On a broad level, my goal is to create a digital screen background that re ...

Transitioning from GeometryUtils.merge() to geometry.merge()

When upgrading from r66 to r67, a message pops up stating: DEPRECATED: GeometryUtils's .merge() has been moved to Geometry. Use geometry.merge( geometry2, matrix, materialIndexOffset ) instead. The transition doesn't seem straightforward beca ...

Implementing conditional validation based on radio button selection with the jQuery validation plugin

This topic goes beyond my basic understanding of jQuery. Here is the form I am working on: http://jsfiddle.net/calebo/E5CRU/ First Question: There are 3 radio buttons, and if the user selects 'other', then the input field for 'other' ...

Showing Text Information from a distant web server's jQuery.getJSON() script

I am currently working on a personal project that involves displaying data from a remote server. The HTML code I am using looks like this: <script> jQuery.getJSON("http://someurl.com/something.cgi? AJZ=1&STOCK=S", function(data) { if (data.inform ...

Passing an array from JavaScript to PHP and then storing it as a .json file

Query I am trying to pass an array to PHP and save it in a data.json file. However, the data.json file is being created but showing Null as output. I have spent about 2 hours on this code, checked numerous solutions on SO, but nothing seems to work. As I ...

Positioning items to align an image in the center with both left and right elements

There's a simple question I have for all you HTML and CSS experts out there. I'm attempting to center align a list next to an image, but I can't seem to figure out how to position the list to the left or right of the image. This is what my ...

The loading of content will happen only after the fadeOut effect

I'm attempting to incorporate content loading between fadeOut and fadeIn. When I execute the code below, the content loads before the fadeOut is complete: $("#contentArea").fadeOut(1000); $("#contentArea").promise().done(); $("#contentArea").load(c ...