I'm trying to highlight a matched name from user search. If the user search is in the array, the matched array item should be highlighted in the text area. How to accomplish this? Here is what I have so far.
function search() {
var list = $('#textArea').val();
var listArr = list.split(',');
$('#textArea').html(listArr);
var userSearch = $('#nameSearch').val();
var i = 0;
while (listArr[i]) {
var res = listArr[i].indexOf(userSearch);
if (res !== -1) {
console.log(userSearch + ' is on the list.');
//highlight matched name in textArea
}
i++;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<textarea class="nameList" name="names" id="textArea" cols="30" rows="10">joe, monica, harry, hanna</textarea>
</div>
<input type="text" id="nameSearch" />
<button onclick="search()">Search By Name</button>
Here is a link to the codepen: