Adjust the color of certain words in real-time

Looking to enhance user input in a textarea? Want to change the color of specific words as they are typed by the user? For example, if you are expecting the user to type:

"My name is Maryam and My age is 24"

and you want to highlight 'My name' and 'My age' in blue as soon as they are typed. Additionally, you want these changes to be reflected inside the textarea itself.

Is there a way to achieve this using CSS, HTML or JavaScript?

Answer №1

Transforming a text area into colored text can be achieved by creatively utilizing CSS and JavaScript. One way to do this is by hiding the textarea element and displaying the colored text in another visible element.

Customize Your Text:

<textarea onkeyup="textChange()"></textarea>
<div id="colorText"></div>

Implement Javascript Function:

var textChange=function(){
    var text=event.target.value;

    if(text.indexOf("My name")>-1){
        text=text.replace("My name", "<span class='blue'>My name</span>");
    }

    if(text.indexOf("my age")>-1){
        text=text.replace("my age", "<span class='blue'>my age</span>");
    }

    document.getElementById("colorText").innerHTML=text;
}

Add Styling with CSS:

.blue{
    color: blue;
}

Answer №2

Changing the color of text within an input field may not be possible, but you can create a preview feature that highlights specific words in different colors. One way to achieve this is by using a Regex statement to scan the text as characters are typed (utilizing a jQuery "Change" event). When certain words are detected, wrap them in html tags for styling in the preview window.

This strategy allows for dynamic coloring of desired words, enhancing the visual presentation of the input data. By applying CSS styles to these tags, you can customize the appearance of each highlighted word.

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

Issue: ENOENT - The specified file or directory, './views/s.ejs', does not exist in Node.js Express

Encountering an error when attempting to render a file from the 'views' directory in the 'routes'. The specific error message is as follows: Error: Valid Login { [Error: ENOENT: no such file or directory, open './views/s ...

Developing a personalized file upload button in React

I have been working on creating a custom <input type="file"> upload button in React. My goal is to display the name of the uploaded file on the button itself after the upload. I encountered some challenges while trying to create a codepen demo, so I ...

What is the proper way to implement a foreach loop with a datalist in HTML?

Struggling to figure out how to create a dynamic drop-down datalist in HTML. I want the options in the drop-down menu to come from an array, rather than hardcoding them one by one. Unfortunately, when I test the code, the drop-down menu appears empty. As a ...

Issue with Vue.js - GET response not being stored in this.data

<!DOCTYPE html> <html> <head> <title>Welcome to Vue</title> <script src="https://unpkg.com/vue/dist/vue.js"></script> </head> <body> <div id="app"> <button v-on:click="sendTime">Lo ...

Trouble with DOM loading due to external JavaScript file reference

I'm having an issue with referencing the jQuery library in a master page. Here is how I am including it: <script type="text/javascript" src="../../Scripts/jquery-1.4.1.js"> After loading the page, I only see a blank screen. The HTML code is th ...

Ways to make ionic slides adhere to column width

I've been immersing myself in learning the latest versions of AngularJS and Ionic through practical application. I am currently working on a page that uses ionic rows and columns to display JSON data. The layout includes a 50/50 column setup, with a t ...

Navigating through JavaScript objects challenge

Having trouble with my JavaScript iteration, why am I not getting the correct result? (I'm new to JS) var data = "data":{ "unknown_name":{ "0":{ "price":"23432", "name":"one" }, ...

I struggle with managing a z-index in conjunction with an absolute position

Check out this codesandbox to see the issue where the 3 dots button is displayed over the menu. Click on the 3 dots to understand the problem. Below is the CSS code for the menu: .more-menu { position: absolute; top: 100%; z-index: 900; float: l ...

ng-disabled not activating after update to data

Creating an application that allows users to upgrade components using in-game currency has been a challenging endeavor for me. Upon clicking the "upgrade" button, an AJAX request is sent to the server to perform the upgrade. Subsequently, another AJAX requ ...

Javascript continues to execute even after the designated element has been eliminated or substituted

I'm currently working on a WordPress auction site using WooCommerce that needs a countdown timer to display when a specific product auction is ending. Below is the JavaScript code for the countdown timer: const getElem = elem => document.q ...

Extracting information from an ENORMOUS Array

Let's start with my code snippet, featuring an array: var UserProfiles = [{ userProfileID: 1, firstName: 'Austin', lastName: 'Hunter', email: 'test', token: '', platform: 'android ...

Customize the color when clicking on a mat-slide-toggle

Whenever I click on the element, a shadow appears in the color that corresponds to the element, which is currently yellow https://i.sstatic.net/21BR4.png I would like to change that shadow to be green instead, but I am unsure of how to do so. Here is my ...

Analyzing: Sending the uploaded file or image to the backend server

Included in my HTML is a form: form.html: <form method="post" enctype="multipart/form-data" action="/"> <input type="file" name="pic" id="imgupload"> </form> <script> document.getElementById("imgupload").onclick = function ...

What happens if I were to move Html.ValidationSummary() outside of the form tag?

Currently, I am developing an asp.net mvc-5 web application that includes the following form structure: @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Contact</h4> <hr ...

Issue with image not fully encompassing div in Bootstrap 5

Currently working on a grid layout with images within the Bootstrap 5 framework. Here is the code snippet for the grid: #project_images img { height: 100%; width: 100%; object-fit: cover; } <section> <div class="container" id ...

Hide the Popup once the data has been successfully submitted to the server

I need assistance with closing a popup after saving the information in the database. I attempted to use the code provided below, however, it did not work as expected. <form id="form1" runat="server" > <div> <img alt ...

Make the text within the CSS style of ".well" wrap around the width

I am looking to style the width of a .well class to be in proportion with the text inside a simple textbox that it contains. What is the best approach to achieve this sizing using CSS? ...

I am looking to apply a loading animation specifically to the ng-select dropdown in an Angular project, without affecting the input field

I have some HTML and TS code that I'd like to share with you. @Component({ selector: 'app-branch', templateUrl: './branch.component.html', styleUrls: ['./branch.component.scss'], animations: [ trigger(' ...

Access numerical values from JSON objects using JavaScript

I need assistance with implementing a JavaScript function to retrieve data from a JSON file and then display it on my webpage. The goal is to iterate through the values in a while loop and dynamically insert them into specific HTML elements. xhttp.onrea ...

When I click on .toggle-menu, I want the data-target to have a toggled class and the other divs to expand

Looking to achieve a functionality where clicking on .toggle-menu will toggle a class on the specified data-target element and expand other divs accordingly. jQuery(document).ready(function() { var windowWidth = jQuery(window).width(); if (win ...