I created a simple program that searches for a value within a div and displays the first location of the word. However, I am looking to enhance it by highlighting all repeated locations of the searched word after pressing the search button.
<html>
<head>
<style type="text/css">
#searchdiv{background-color: #999999;left: 277px;margin: 39px 0 0 84px;padding:5px;width:500px;align:center;}
</style>
<script type="text/javascript">
function search()
{
var keyword=document.getElementById("keyword").value;
var str=document.getElementById("area").innerHTML;
var n=str.search(keyword);
alert('Location is :'+n);
}
</script>
<body>
<div id="searchdiv">
<input type="text" id="keyword" name="text" size="70" />
<input type="button" value="search" id="btn" name="button" onclick="search()"/>
<br>
<div style="width:444px;" id="area" name="textarea">rajendra kumar rahar from jhunjhunu</div>
</div>
</body>
<html>
I have attempted multiple times but haven't been able to figure out how to highlight specific text within a div using JavaScript... Can anyone provide assistance? If you have any solution, please share with me