hi i don't know it is wright place for it or not.
i am new in javascript programming.
i am try to implement find and replace functionality.
in it i want to search keyword in particular div only. not other part of the page.
i have implement following code

function findNext()
{
   if(parentWindow )//if parent window found
   {

        if(parentWindow.checkElements.length>0)// div elements present
        //if(checkElements.length>0)// div elements present
        {   
            //Clear div
            clearFind(parentWindow.document.getElementById(parentWindow.checkElements[currentDivIdIndex]));
            //clearFind(document.getElementById(checkElements[currentDivIdIndex]));
           // parentWindow.document.getElementById(parentWindow.checkElements[currentDivIdIndex]).innerHTML = divTag.innerHTML;

            var txtFind = document.getElementById("txtFind");
            if(txtFind!=null)
            {
                if(txtKeyWord != txtFind.value.replace(/>/g,"&gt;").replace(/</g,"&lt;"))
                {
                    txtKeyWord = txtFind.value.replace(/>/g,"&gt;").replace(/</g,"&lt;");//text to be search
                    strTagedKeyWord = highlightWord(txtKeyWord);//highlighted text
                    currentDivIdIndex = 0;//set current div to first div
                    nextSelectedIndex = 1;//select first word
                }

                if(txtKeyWord == "")
                {
                    alert("Search String not specified...");
                    document.getElementById("txtFind").focus();
                    return;
                }

                //start from current word container Div
                for(var cnt=currentDivIdIndex; cnt<parentWindow.checkElements.length; cnt++)
                {
                    //var divFind = document.getElementById(checkElements[cnt]);//get div references
                    var divFind = parentWindow.document.getElementById(parentWindow.checkElements[cnt]);//get div references

                    if(divFind != null)
                    {   
                        var strSearch = divFind.innerHTML;
                        var matchedWords = Array();
                        var strSubArray = Array();
                        matchedWords = strSearch.match(new RegExp(txtKeyWord,"ig"));
                        if(matchedWords != null)//if Keyword is found
                        {
                            strSubArray = strSearch.split(new RegExp(txtKeyWord,"ig"));//make array of string by breaking string into substring by means of Keyword occurance

                            var WordCount=1;// occurance counter
                            if(!(matchedWords.length < strSubArray.length))//if search found at start or end of string
                            {
                                var strTemp = new Array("");
                                var tmpCnt=0;

                                if(matchedWords.length > strSubArray.length)//if search criteria is found at both end
                                {
                                    for(tmpCnt=0;tmpCnt<strSubArray.length;tmpCnt++)
                                    {
                                        strTemp[strTemp.length]=strSubArray[tmpCnt];
                                    }
                                    strTemp[strTemp.length]="";
                                }
                                else
                                {
                                    //alert(strSearch.indexOf(matchedWords[0])); 
                                    if(strSearch.indexOf(matchedWords[0])==0)//if search criteria is found at starting of string
                                    {
                                        for(tmpCnt=0;tmpCnt<strSubArray.length;tmpCnt++)
                                        {
                                            strTemp[strTemp.length]=strSubArray[tmpCnt];
                                        }
                                    }
                                    else
                                    {
                                        strTemp=strSubArray;
                                        strTemp[strTemp.length]="";
                                    }
                                }
                                strSubArray=strTemp;
                            }

                            var strHighlight = strSubArray[0];//place first substring as it is
                            var bFlgFind =true;

                            for(WordCount=1; WordCount < strSubArray.length;WordCount++)
                            {
                                if(bFlgFind && WordCount == nextSelectedIndex)
                                {
                                    if( strSubArray[WordCount-1].lastIndexOf('<') <= strSubArray[WordCount-1].lastIndexOf('>') )
                                    {
                                        strHighlight += highlightWord(matchedWords[WordCount-1]) + strSubArray[WordCount];
                                        bFlgFind = false;
                                    }
                                    else
                                    {
                                        strHighlight += matchedWords[WordCount-1] + strSubArray[WordCount];
                                    }
                                    //strHighlight += strTagedKeyWord + strSubArray[WordCount];
                                    bFlgFind = false;
                                    nextSelectedIndex++;
                                }
                                else
                                    strHighlight += matchedWords[WordCount-1] + strSubArray[WordCount];
                            }
                            divFind.innerHTML = strHighlight;

                            if(!bFlgFind)
                            {
                                document.getElementById("btnReplace").className="BtnMedium";
                                document.getElementById("btnReplace").disabled = false;
                                return;
                            }
                        }

                        currentDivIdIndex++;
                        nextSelectedIndex=1;
                    }
                }
            }
            else
            {
                alert("FindText Element not found..");
            }
            if(currentDivIdIndex >= parentWindow.checkElements.length)
            {
               //alert("Search Complete");
               currentDivIdIndex = 0;//set current div to first div
               nextSelectedIndex = 1;
               document.getElementById("btnReplace").className="BtnDMedium";
               document.getElementById("btnReplace").disabled = true;
            }
        }
        else
        {
            alert("String not found....");
        }
   }
   else
   {
        alert("Parent not Found...");
   }
}

i am facing one problem in it, that when div innerhtml is like
"this is test <font>string</font>." and search for "string." it will fail and show string like this
"this is test <font>string/font>."
so anybody help me which changes i have to do in my code to remove this bug
thanks for help

Recommended Answers

All 3 Replies

hi i don't know it is wright place for it or not.
i am new in javascript programming.
i am try to implement find and replace functionality.
in it i want to search keyword in particular div only. not other part of the page.
i have implement following code
. . .
i am facing one problem in it, that when div innerhtml is like
"this is test <font>string</font>." and search for "string." it will fail and show string like this
"this is test <font>string/font>."
so anybody help me which changes i have to do in my code to remove this bug
thanks for help

Please use: [B]targetElement.innerText[/B] IE / Safari / Chrome / Opera / Konqueror

For Firefox, use: [B].texContent[/B] or for crossbrowser cure the innerHTML string with [B]str2search = targetElement.innerText||targetElement.textContent[/B]

hi thanks for reply
i tried it but it is fail in case when i have place field as it is without any changes. if i store innerhtml in variable but its fail during replace.
but thanks again for giving time.

i got solution using "CreateTextRange()"

That's not true! It can't fail in any possible content.
I never told you to store in an innerHTML. I told you to:

Please use: targetElement.innerText

And than I told you to use cross-browser normalization

str2search = targetElement.innerText||targetElement.textContent

That is, - to normalize the captured content, without having to modify your existing script structure before running the search on it.
And this live example can prove it: javascript: var a = document.getElementsByTagName('textarea')[0]; str2search = a.innerText||a.textContent; document.writeln(str2search); right from your address-bar in this forum page. [in the reply page, of course!!!]

Anyway, the "createTextRange" is not very widely supported - but thanks for asking.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.