Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Developer
TheFlowFX
Posts: 166
Registered: ‎02-15-2012
My Carrier: Bags are useful

Array search Javascript

<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>

 Here is my code. 

 

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

If any post helps you please click the Like Button below the post(s) that helped you.
2. Please resolve your thread by marking the post "Solution?" which solved it for you!

Please use plain text.
Developer
TheFlowFX
Posts: 166
Registered: ‎02-15-2012
My Carrier: Bags are useful

Re: Array search Javascript

bump

If any post helps you please click the Like Button below the post(s) that helped you.
2. Please resolve your thread by marking the post "Solution?" which solved it for you!

Please use plain text.
BlackBerry Development Advisor
haahmad
Posts: 6
Registered: ‎10-08-2012
My Carrier: Telus

Re: Array search Javascript

Hi TheFlowFX, 

 

From what I understand, you have an array of keywords and you want to search an arbitrary string for words that match your keywords, correct?

 

Here is what I hacked out in really fast in web inspector:

 

var keywords = ["car", "bug"];
 
var string = "my car is not the size of a bug";
var matches = [];
string.split(" ").forEach(function (word) {
    if (keywords.indexOf(word) > -1) {
        matches.push(word);
    }
});
 
//matches should now be: ["car", "bug"]
 
"split" converts your space seperated string to an array of words, you can then loop through the array of words and see if they are in your array of keywords by checking that indexOf returns a non-negative index.
Please use plain text.