ArticleZip > Change Color Of Specific Words In Textarea

Change Color Of Specific Words In Textarea

Do you ever wonder how to make your text stand out in a sea of words? Well, look no further because today, we're diving into the exciting realm of changing the color of specific words in a textarea. This nifty trick can add an extra flair to your text and make certain words pop to grab attention.

To start off, let's get technical. When it comes to changing the color of specific words in a textarea, we'll be venturing into the realm of HTML and CSS. These two powerhouses of the web are going to be our trusty tools to achieve our goal.

First things first, you'll need to create a textarea in your HTML document. This is where your text will be displayed. You can do this by using the following code snippet:

Html

<textarea id="myTextarea">Hello, world! This is a test.</textarea>

In this snippet, we're creating a simple textarea with an id of "myTextarea" and some sample text inside. Now, we're ready to jazz up our text by changing the color of specific words.

The next step involves adding some CSS magic. We'll use a little CSS trickery to target specific words and give them a fresh new look. Here's how you can do it:

Css

#myTextarea::selection {
    background: yellow;
}

#myTextarea::-webkit-input-placeholder {
    color: red;
}

In this CSS snippet, we're targeting the textarea with the id "myTextarea" and using the ::selection pseudo-element to change the background color of the selected text to yellow. Additionally, we're using the ::-webkit-input-placeholder pseudo-element to change the color of the placeholder text to red. These are just a couple of examples of how you can customize the appearance of specific words in your textarea.

Now, let's take it up a notch and target specific words within the textarea. To achieve this, we can use a little JavaScript wizardry. Here's a snippet to get you started:

Javascript

var text = document.getElementById('myTextarea');
var words = ['world', 'test'];

words.forEach(function(word) {
    var pattern = new RegExp(word, 'g');
    text.innerHTML = text.innerHTML.replace(pattern, '<span style="color:blue">' + word + '</span>');
});

In this JavaScript snippet, we're targeting the words "world" and "test" within the textarea and changing their color to blue. You can customize the words and colors to suit your preferences.

And there you have it! With a bit of HTML, CSS, and JavaScript, you can easily change the color of specific words in a textarea. So go ahead, have some fun with it, and give your text a vibrant makeover! Remember, the possibilities are endless when it comes to showcasing your creativity in coding. Happy coding!

×