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,">").replace(/</g,"<"))
{
txtKeyWord = txtFind.value.replace(/>/g,">").replace(/</g,"<");//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