View Full Version : Array Searching

11-12-2012, 08:21 PM
Here is my code:

.found {background-color: yellow; font-weight: bold; color: blue;}
#results {border: solid blue 2px; width: 200px;}

function searchText(){
outStr = ""
inStr = document.myForm.myText.value
searchStr = document.myForm.searchField.value
sLen = searchStr.length
foundCount = 0
for (x=0; x<inStr.length; x++){
if (inStr.substr(x,sLen) == searchStr){
outStr = outStr + "<span class='found'>" + searchStr + "</span>"
foundCount ++
x += sLen-1
outStr += inStr.substr(x,1)
document.getElementById("results").innerHTML = "<b>" + foundCount + " </b>results found.<br><br> " + outStr

<form name="myForm">
<textarea name="myText">
This is my sample text area. This is where I will place the sample text. I really hope that this works the way I planned.
<input name="searchField"><input type=button onClick="searchText()" value="Search Text">
<div id="results"></div>

What I want to do is have an array of words in a seperate Javascript file. So then whenever the user types something into the text are and hit's 'Search text', it will highlight the words from the array if it exists.

Eg. Hello[0] is in the array.

The user types in the text area 'Hello my name is John'

The word Hello is highlighted since it exists in the array.

If anyone can tell me how to do this, Greatly appreciated.


Philip M
11-12-2012, 11:23 PM
A textarea can only support plain text.

But have a look at



All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

Old Pedant
11-12-2012, 11:41 PM
Not hard.

But you can't highlight it in the <textarea>. Textarea's don't allow HTML tags to change the appearance of the text therein.

You would have to display the result in a <div> or <span>.

Is that okay?

11-13-2012, 12:01 AM
Yes this is brilliant. Thank you so much. Just another quick question:

How can I replace this in my code:

var _terms = ['January', 'February', 'March']; // YOUR SEARCH TERMS GO HERE //

And have a separate JS file with an array of many words.

The rest of the code is here:


Old Pedant
11-13-2012, 02:03 AM
Just replace all uses of _terms with the name of your array.

Or simply do

var _terms = theNameOfYourArray;

Philip M
11-13-2012, 09:06 AM
This example may help to move you forward.


<style type="text/css">
.highLight1 {font-family: arial; font-size: 12pt; color: #FF0000; font-weight: bold; background-color:#b0c4de; }


<body onload = "findwords()">

<textarea id = "myta" rows = "15" cols = "75" onkeyup = "findwords()">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ipsum "leo", scelerisque at dapibus ac, consectetur vel ipsum. Morbi et metus ut diam molestie ullamcorper. Suspendisse rutrum semper semper. Donec volutpat neque in lorem tempus scelerisque. Curabitur dignissim rhoncus quam ac suscipit. Donec viverra quam lobortis neque porta a sagittis urna tristique. Suspendisse nec lacus nisi. Pellentesque fermentum massa sit amet magna hendrerit vestibulum? Sed elit libero, scelerisque eu eleifend ut, interdum gravida nunc. Etiam ut nisi sapien, et tempus sem. Nam vel mi est. Mauris congue felis ut ante bibendum vehicula. Nullam nec sapien arcu, eget cursus lorem. Donec blandit, dolor tristique ornare dictum, arcu sapien vulputate dolor, et placerat risus odio ut magna. Ut magna Mauris, pellentesque at ultricies vitae, fermentum vitae dolor. </textarea>
<div id = "hText"></div>

<script type = "text/javascript">

// Note textarea can only support plain text so highlighted results must be in a div

function findwords() {

var words = ["lorem", "dolor", "massa", "magna", "mauris"];
var text = document.getElementById("myta").value;
document.getElementById("hText").innerHTML = text;
text = text.split(" ");

for (var i=0; i<text.length; i++) {
for (var j=0; j<words.length; j++) {
var temp = text[i];
var templc = temp.toLowerCase();
templc = templc.replace(/[^a-z0-9]/gi,""); // strip punctuation
if (templc == words[j].toLowerCase()) { // find whole words only
var repval = "<span class='highLight1'> " + temp + " <\/span>"
text[i] = repval;
var output = text.join(" ");




11-13-2012, 09:41 AM
I guess, he's looking for something like autocomplete.
There are many code snippets on the internet.
Google: autocomplete.

Or do you need something different?