TheFlowFX
11-12-2012, 07:21 PM
Here is my code:
<style>
.found {background-color: yellow; font-weight: bold; color: blue;}
#results {border: solid blue 2px; width: 200px;}
</style>
<script>
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
}
else{
outStr += inStr.substr(x,1)
}
}
document.getElementById("results").innerHTML = "<b>" + foundCount + " </b>results found.<br><br> " + outStr
}
</script>
<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.
</textarea>
<br>
<input name="searchField"><input type=button onClick="searchText()" value="Search Text">
</form>
Results:
<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.
Regards,
Flow
<style>
.found {background-color: yellow; font-weight: bold; color: blue;}
#results {border: solid blue 2px; width: 200px;}
</style>
<script>
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
}
else{
outStr += inStr.substr(x,1)
}
}
document.getElementById("results").innerHTML = "<b>" + foundCount + " </b>results found.<br><br> " + outStr
}
</script>
<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.
</textarea>
<br>
<input name="searchField"><input type=button onClick="searchText()" value="Search Text">
</form>
Results:
<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.
Regards,
Flow