Pyreal Pyreal - 1 year ago 51
CSS Question

How do you highlight typed text when it's not static?

Example of what I'm going for.

I have two Textareas. One holds a list of single words, one per line, this list will not be the same every time.

My second text area will be used for input by the user. My goal is to highlight words in the first text area when that word is typed into the second text area. I've attached an example photo showing what I wish to accomplish.


My code is just pushing a list of words into the first Text area and separating them. I'm looking for help on how to highlight the desired words.

<textarea class="col-md-3" id="inputArray1" rows="20" cols="10" placeholder="Input1"></textarea>
<textarea class="col-md-3" id="inputArray2" rows="20" cols="10" placeholder="Input2"></textarea>

//holding input values for text area
var InputVar = document.getElementById("inputArray1").value;
var InputVar2 = document.getElementById("inputArray2").value;

//holding output values for text area
var OutputVar = document.getElementById("outputArray1");
var OutputVar2 = document.getElementById("outputArray2");

// takes string and breaks into substring array of words
var SplitString = InputVar.split(/[\s]+/g);

//displays list of words in 2nd text area
OutputVar.value = SplitString;

Answer Source

Since you tagged jQuery, here is a basic function that checks if any user words match any of the listed words:

$("#userArray").on('change keyup paste', function() {
  var input = $(this).val().split("\n");
  $('#listArray span').each(function() {
    if ($.inArray($(this).text(), input) != -1) {
#list_input > div { border:4px solid; padding: 1em; margin: 1em auto; }
#listArray { overflow: auto; }
#listArray span { display: block; float: left; clear: left; padding:4px; margin:1px; }
#listArray { background: yellow; }
#userArray { width: 100%; height: 150px; border: none; }
<script src=""></script>
<div class="container" id="list_input">
  <div id="listArray">
    <textarea id="userArray" placeholder="enter words..."></textarea>