Forum Moderators: open

Message Too Old, No Replies

Search Program

Trouble Using Node Iteration

         

moonshadow

12:43 am on Jul 21, 2004 (gmt 0)

10+ Year Member



I am trying to write a "search" program in JavaScript. The function finds the search term within all test nodes and makes them bold and also places anchors next to them for later indexing (or atleast that's my intention)... please help me determine what I'm doing wrong/right. Thanks in advance! Peace.


<html>
<head><title>Search Engine Test 1</title></head>
<body>
<script language="javascript" type="text/javascript"><!--
function findBoldAnchor( node ){
if( node.nodeType == 3 ){
var text = node.data ;
var searchTerm = document.searchForm.searchTerm.value ;
var offset = new Array() ;
for( i = 0; i < text.split( searchTerm ).length; i++ ){
if( i == 0 )
offset[i] = text.indexOf( searchTerm ) ;
else
offset[i] = text.indexOf( searchTerm, offset[ i - 1 ] + searchTerm.length ) ;
node.replaceData( offset[i], searchTerm.length, '<a name="' + searchTerm + '' + i + '"></a><b>' + searchTerm + '</b>' ) ;
}
}
else{
var kids = node.childNodes ;
var numkids = kids.length ;
for( var j = 0; j!= numkids; j++ ){
findBoldAnchor( kids[j] ) ;
}
}
document.getElementById( 'Output' ).innerHTML = document.getElementsByTagName( 'body' )[0].innerHTML ;
}
//-->
</script>
<p>This will be cow a paragraph of text <i>cow</i> that will be used in a search test.</p>
<table>
<tr>
<td>The search term will be cow and the object is to get those words emboldened and anchored</td>
<td>Not all fields will have the search term <i>in them</i> but will have random markup</td>
</tr>
</table>
<br/><br/>
<form action="javascript: window.navigate(window.location);" metho="post" id="searchForm" name="searchForm">
<input type="text" value="" size="20" id="searchTerm" name="searchTerm"/>
<input type="submit" value="search" onsubmit="findBoldAndAnchor( document.body ); return true;" />
</form>
<span id="Output"></span>
</body>
</html>

Try using "cow" as the search term. Thanks again!

moonshadow

1:04 am on Jul 21, 2004 (gmt 0)

10+ Year Member



I fixed the onsubmit attribute of the submit button to read onsubmit="findBoldAnchor(..." but it still doesn't work. Sorry for the typo.