When a specific string is found in a textbox, trigger a function in JavaScript

Is there a way to search for a specific string within a large text box that contains 200 words? I would like to create a function that can color the string. For example, if the sentence "my dog is happy" is in the textbox and I want the word "dog" to be displayed in red when a button is pressed or through another method. Is this achievable?

Answer №1

A feasible option is available. Avoid utilizing a text box or text area, instead opt for a div that includes contenteditable = "true":

<div id="editableDiv" class="editable" contenteditable="true">
    This particular sentence features the word 'dog'.<br />
    Modifying the contents of this div is achievable.
</div>
<button id="highlightBtn">Highlight "dog"</button>
<script type="text/javascript">
   highlightBtn.onclick = function() {
         var elem = document.getElementById('editableDiv');
         elem.innerHTML = elem.innerHTML.replace(/dog/g, 
                            '<span class="redText">dog</span>');
   }
 </script>

Ensure to include the classes redText and editable in your stylesheet:

.editable {
    padding: 5px;
    border: dashed 1px black;
}
.redText {
    color: red;
}

JSFiddle: http://jsfiddle.net/ProgramFOX/UMMPh/

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

Uncovering the secrets of locating and deleting arrays with the power of jQuery

Is there a way to extract data from an array list and then have it automatically removed once displayed? let fruits = ["Banana", "Orange", "Watermelon"]; For instance, if I want to select the Watermelon element from the array. After retrieving and display ...

What is the best way to utilize Python in order to transform a large amount of unicode into characters to create a string-like appearance?

In JavaScript, I've managed to do this: eval(String.fromCharCode(102,117,110,99,116,105,111,110,32,99,104,101,99,107,40,41,123,13,10,09,118,97,114,32,97,32,61,32,39,100,52,103,39,59,13,10,09,105,102,40,100,111,99,117,109,101,110,116,46,103,101,116,69 ...

Mastering Tooltip Placement Using CSS or JavaScript

I have been working on creating a CSS-only tooltip for a web application and so far I have managed to create some useful tooltips with different classes: tooltip-up tooltip-down tooltip-left tooltip-right The distinguishing factors between them are t ...

Trouble with displaying class object array in Angular 5 using ngFor loop

I am attempting to loop through an array of class objects to populate a dropdown menu in Angular 5. However, the dropdown menu is not showing any options and there are no errors appearing in the browser console. The object array is created based on this m ...

Issue with Google map displaying incorrectly when attempting to print

I am experiencing an issue with printing a Google Map using the JavaScript API V3. The problem I am facing is that the map is split into two parts, with the top and bottom sections overlapping each other. Here is the code snippet: var geocoder; var ...

Escaping HTML code within a Mustache template is crucial to prevent

I have a JavaScript array that looks like this: [button: "M'AVERTIR"] When I attempt to display the string in a mustache template, it appears in the following format: M&#039;AVERTIR Here's the code for my mustache template: {{=<% %> ...

Using sql.js to import CSV files into SQLite databases

Is it possible to import a CSV file into a SQLite database using JavaScript instead of the command line? I am currently utilizing the sql.js library to work with SQLite in Javascript. I appreciate any help or suggestions on how to accomplish this task. Th ...

Developing an Addon for Firefox using XUL and JavaScript

I am interested in creating a Firefox Addon that dynamically generates a button in the webpage's DOM when a specific page like www.google.com is loaded. This button, when clicked, will redirect to another page such as www.stackoverflow.com. It is imp ...

Chosen option within the AntD Select component

Using the AntD select component, there seems to be an issue where it displays "id" instead of "name" when selecting options. Is there a solution available for this problem? const MyComponent = () => { const data = [ { id: "5c83c2d ...

Ways to ensure the final unchecked checkbox stays in the checked state

I currently have a set of checkboxes that are pre-selected. However, I would like to ensure that if all checkboxes except one are unchecked, and an attempt is made to uncheck that final checked checkbox, an error message will appear and the checkbox will n ...

What are the ramifications of using the 'new' keyword in JavaScript?

Currently, I am in the process of developing a plugin for jQuery, and I am encountering a JSLint error that redirects to JSLint: Problem at line 80 character 45: Do not use 'new' for side effects. (new jQuery.fasterTrim(this, options)); Despit ...

"Utilizing ng class with an array of objects: A step-by-step guide

I am facing a scenario in which my response appears as follows: "currency" : [ { "_id" : ObjectId("584aad5d3e2537613e5f4c39"), "name" : "USD" } ], I need to enable my checkbox based on the currency name. I attempted the followi ...

Using Angular Material for creating tabs with identical content

I am using material with angularjs and have encountered an issue with my two md-tabs. Both tabs have a similar DOM structure, but contain some unique content. Instead of duplicating the common DOM twice, I am looking for an alternative solution. Is it poss ...

"Upon submitting a form in React JS, the components will automatically trigger a

Within my application, there is a Mobx storage in conjunction with a modal window component. The form within the modal window allows me to collect all the properties and push them into an array named 'cart' within the storage as an object. Take a ...

I'm finding it difficult to decide which comparison operators to utilize for my JavaScript game. Experimenting with both the "&&" and "||" options has left me unsure of which

In this scenario: If the user enters a number in the text box, such as 2 (paper) or 3 (scissors), the "ComputerChoice" will randomly generate a number between 1 and 3. The goal is to make the computer understand and react to different scenarios... For exa ...

Utilizing ag-grid in React to render boolean values as text instead of checkboxes

When working with AG-Grid-react, is there a way to show boolean values as text in a column instead of checkboxes by default? I need help finding a property that can achieve this. ...

executing a Prisma database migration with various schemas

I am currently immersed in a Prisma project where my goal is to create a node module that can be utilized by other projects. The challenge now is to ensure that the database stays synchronized with the models and the primary project, so all testing platfor ...

Tips for displaying the overlay in a jQuery UI modal dialog

The demonstration shows that the overlay is displayed correctly and all elements below it are disabled: <div class="ui-widget-overlay" style="width: 1920px; height: 650px; z-index: 1001;"></div> However, on my webpage, I am still able to inte ...

When the overflow is set to visible, the background color vanishes

Here is the CSS code I have written; #container { width: 1300px; background-color:green; margin:0 auto; overflow:hidden; } #menu { float:left; width:20%; background-color: yellow; } Even after extensive searching on Google, I ...

Tips for effectively utilizing Mongoose models within Next.js

Currently, I am in the process of developing a Next.js application using TypeScript and MongoDB/Mongoose. Lately, I encountered an issue related to Mongoose models where they were attempting to overwrite the Model every time it was utilized. Here is the c ...