Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Next Occurrence in Search

Status
Not open for further replies.

JBorges

Technical User
Jan 12, 2009
57
DK
Hello.

I have this JS code which I found on the web. It searches for a text string the user inserts into a textfield. I would like to be able to tap a button that goes either to next or previous occurrence. Setting up the GUI is not a problem, it's just how to code the next/previous function in JS. (I'm developing for iOS using a UIWebView, but that shouldn't be an issue here, since the code revolves around JS):

Code:
function HighlightAllOccurencesOfStringForElement(element,keyword) {
    if (element) {
        if (element.nodeType == 3) { // Text node
            while (true) {
                var value = element.nodeValue;  // Search for keyword in text node
                var idx = value.toLowerCase().indexOf(keyword);
                
                if (idx < 0) break;             // not found, abort
                
                var span = document.createElement("span");
                var text = document.createTextNode(value.substr(idx,keyword.length));
                span.appendChild(text);
                span.setAttribute("class","MyAppHighlight");
                text = document.createTextNode(value.substr(idx+keyword.length));
                element.deleteData(idx, value.length - idx);
                var next = element.nextSibling;
                element.parentNode.insertBefore(span, next);
                element.parentNode.insertBefore(text, next);
                element = text;
                span.scrollIntoView(); // THIS SCROLLS TO THE FIRST SEARCH OCCURRENCE
                span.style.backgroundColor="yellow"; // must set the CSS style after scrolling to first search instance
                span.style.color="black";
                SearchResultCount++;	// update the counter

            }
        } else if (element.nodeType == 1) { // Element node
            if (element.style.display != "none" && element.nodeName.toLowerCase() != 'select') {
                for (var i=element.childNodes.length-1; i>=0; i--) {
                    HighlightAllOccurencesOfStringForElement(element.childNodes[i],keyword);
                }
            }
        }
    }
}
 
not tested this but it might work

Code:
var currentHighlightedSpan;

function scrollToNext(){
  var spans = document.getElementsByClassName('MyAppHighlight');
  if (spans[currentHighlightedSpan + 1]){
    spans[currentHighlightedSpan + 1].scrollIntoView();
    currentHighlightedSpan++;
  }
}

function scrollToPrev(){
  var spans = document.getElementsByClassName('MyAppHighlight');
  if (spans[currentHighlightedSpan 1 1]){
    spans[currentHighlightedSpan - 1].scrollIntoView();
    currentHighlightedSpan--;
  }
}

you might want to add some checks that dis/enables the buttons depending on whether you are at the last/first highlighted search term.
 
Thank you jpadie.

I coulnd't get it to work though, in the meantime I solved it this way:

Add this line
Code:
a.push(span);
before this line
Code:
SearchResultCount++;

Then in objective c add this a method to call JS:

Code:
currentPosition = currentPosition - 1; // currentPosition is an integer 
NSString *nextScrollPosition = [NSString stringWithFormat:@"a[%d].scrollIntoView()", currentPosition];
[webView stringByEvaluatingJavaScriptFromString:nextScrollPosition];

I realize that this is not an entirely pure JS entry, but I hope it can help others with similar issues, or to adapt the code to their programming language.
 
this line
Code:
if (spans[currentHighlightedSpan 1 1]){
had a typo. the first 1 should have been a minus sign.
 
Thanks jpadie.

I saw that and corrected but I still can't get it to work. As mentioned I have figured out how to do it and it works very well. Thank you for taking the time to reply.
 
Just unravel your objective c into JavaScript.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top