I have a html
code where I want the text I type in an input textbox
to be taken as input, saved into an object, and be matched as keyword
when I type in a simulated textarea
.
Example:
<tr>
<td>KEYWORD:</td>
<td><input type="text" name="key_word" value="" id="tm"></td>
</tr>
My text area where I type text:
<div class="container">
<textarea id="myTextArea"></textarea>
<div class="backdrop">
<div class="colors">
</div>
</div>
</div>
Javascript code:
const color =
{
"connected ..":"green",
"connected .....": "green",
"connection failure .....": "red"
};
let textArea = document.getElementById("myTextArea");
let colorsArea = document.querySelector(".colors");
let backdrop = document.querySelector(".backdrop");
// Event listeners.
textArea.addEventListener("input", function()
{
colorsArea.innerHTML = applyColors(textArea.value);
});
textArea.addEventListener("scroll", function()
{
backdrop.scrollTop = textArea.scrollTop;
});
function applyColors(text)
{
let re = new RegExp(Object.keys(color).join("|"), "gi");
return text.replace(re, function(m)
{
let c = color[m.toLowerCase()];
return `<spam style="color:${c}">${m}</spam>`;
});
}
So, the scenario is like this: when I type the keyword
in the input textbox, like "connected"
for example, then this "connected"
keyword should be passed as match condition to the above javascript. So whenever I type the keyword later in the textarea
, that keyword
should get the green color in the textarea
.
This is the code where I want the textbox value to be stored, So I can get rid of hard coded data:
const color =
{
"connected ..":"green",
"connected .....": "green",
"connection failure .....": "red"
};