I am making a program to search for characters in the desired . I need to color the specific character that i found in text area. How can I colo
You can't change it in a textarea
because textarea
only contains pure text, to add format to words yo ufound you need to surround these matched words within html elements, so this why it won't work, as in this JSFiddle, just another way of doing it, you can search for multiple words if you separate them by a space in the search box like item1 item item3
:
var div = $('#text'), kw,keywords, i, j;
$('#btn').on('click', function (event) {
event.preventDefault();
console.clear();
kw = $('#keywords').val();
keywords = kw.split(" ");
text = div.text();
text = text.replace(/,|\.|\?/i, "");
text = text.split(" ");
for (i = 0; i < text.length; i++) {
for (j = 0; j < keywords.length; j++) {
if (text[i] == keywords[j]) {
text[i] = '' + text[i] + '';
}
}
}
text = text.join(" ");
div.html(text);
});
div {
width:400px;
height:250px;
display:block;
overflow:hidden;
border:lightgrey 2px inset;
padding:1px;
overflow:hidden;
overflow-y:scroll;
}
.hl{
background-color:orange;
padding:2px 3px;
}