I am working on a program to search for specific characters within a designated <textarea>
element. I want to be able to highlight or color the characters that match the search criteria within the text area. How can I achieve this effect and color the character(s) within the <textarea>
? For example, if a user enters 'a' in the input field, then all instances of 'a' within the <textarea>
should be colored red.
HTML
<form method="post" name="searching" onSubmit="return check(this)">
<table border="0" cellpadding="10px" align="center">
<tr><td width="114">
<label><b>Text</b></label></td>
<td width="287">
<textarea name="para" cols="30" rows="10"></textarea>
</td>
</tr>
<tr>
<td>
<label><b>Alphabet</b></label>
</td>
<td><input type="text" name="character" title="Enter Character">
</tr>
<tr>
<td colspan="2" align="center">
<input id="btn" type="submit" name="submit" value="Search">
</td>
</tr>
</table>
</form>
JS
<script language="javascript">
function check(form)
{
if(form.para.value==""){
alert("No text is available for search!!");
return false;
}
if(form.character.value=="")
{
alert("Search keyword is not Enter!!");
return false;
}
para=new Array();
index=new Array();
keyword=form.character.value;
para=form.para.value;
found=0;
k=0;
for(i=0; i<para.length;i++)
{
if(keyword==para[i]){
found+=1;
$(document).ready(function(e) {
$("textarea:eq(i)").css("color","#FF0000");
});
index[k++]=i;
}
}
if(found!=0){
alert(found+" times "+keyword+" in text");
alert("Index of alphabet: "+index);
return false;
}
else{
alert("Not found in the Text!!");
return false;
}
}
</script>
I am open to any solutions, whether they involve CSS, HTML, JS, or jQuery. Thank you.