• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

Searchbox to search only a specific div and not other div's on the page.

I have a .asp page i created with a search field box.  However I only want it to search the div below it and not search any other div's on the page.

I need it to search <div id="maininfo_2"> and ignore <div class="maininfo"> and any other div.

.ASP Page-------------------------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ LANGUAGE="VBSCRIPT" %>
<%

Response.Buffer = TRUE
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = 0


%>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>IBEW Local 347 Benefits Web Site</title>
<link href="../main.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="favicon.ico" />
<link rel="icon" type="image/ico" href="favicon.ico" />

<!-- #include file = "..\includes\SEARCH.INC"-->
</head>
<body>
<div id="container">
<div id="masthead">
  
<!-- end #menu -->
<div id="header"></div>
<!-- end #header -->
<!-- #include file = "..\includes\MENU.INC"-->

</div>
<!-- end #masthead -->
<div id="content">
	
	<div id="posts">
		<div id="post">
		<br />
		 <h2>
		    Health &amp; Welfare </h2>
		 <br />
		    <div class="maininfo"><img src="../images/ladyinfield.jpg"/><br />
		      <br />
		      <p align="left">Experts Exchange is a technology help website. Our experts are real people with real-world technology experience from around the globe: Microsoft MVPs, IT consultants and many more. They volunteer their time to provide tech support in our patented Q&A forums. If you've got a technology problem--big or small--we can help you solve it. 
              </p>
			  
			
				
		</div>
	</div>

		
	</div>
	
	
	
	<div id="maininfo_2">
	
		  <h2 align="left">Frequent&nbsp;Questions &amp; Answers&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</h2>
		  
	  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean tincidunt mattis magna ac egestas. Maecenas mi felis, ullamcorper vel tincidunt id, bibendum quis velit. Nulla quis augue sem. Aenean porttitor mattis arcu, ut semper sapien iaculis rutrum. Maecenas sodales aliquet mollis. Nunc ac lorem vitae nibh fermentum adipiscing quis sed lorem. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Suspendisse potenti. Nam varius nunc blandit diam pharetra interdum eget quis neque. 
Vivamus sed sapien et libero convallis pulvinar at id ante. Donec pharetra egestas laoreet. Nam tempor eleifend augue, a commodo metus ornare id. Nam fringilla ultricies arcu, ac placerat mi sagittis eget. Suspendisse consequat lorem enim, vitae sodales arcu. Curabitur feugiat ligula dictum tortor suscipit ut bibendum ipsum tempus. Cras vitae lacus ut ante tempor dignissim a id massa. 
Quisque sagittis, nisl in venenatis laoreet, dui purus sodales lorem, vitae iaculis tellus leo eget felis. Curabitur eu arcu vel lectus commodo porta. Fusce id nulla nec ipsum vehicula scelerisque. Donec at eros odio, gravida volutpat purus. Vivamus quis nunc mauris. Duis mollis, nibh a varius euismod, sapien odio ullamcorper libero, sed ornare est felis vel libero. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Etiam feugiat vestibulum rutrum. Vivamus adipiscing dui non turpis tempus dapibus. Vivamus porta egestas lorem quis tempor. Praesent viverra felis tincidunt erat molestie sit amet eleifend justo feugiat. 
Duis dapibus ipsum nec leo sagittis suscipit. Curabitur iaculis velit vitae mi scelerisque a sagittis ligula eleifend. Mauris sit amet sapien ipsum. Proin ornare laoreet odio ac commodo. Phasellus vitae nunc vel nisi sodales congue aliquet ut lectus. Proin ac nisi ac velit volutpat ultrices id vel eros. Aenean placerat, mauris a hendrerit consectetur, velit eros condimentum leo, vel aliquet felis purus cursus lacus. Donec vulputate porta dui, ac pharetra erat luctus in. Integer sapien ipsum, imperdiet ac blandit non, hendrerit sed purus. Quisque tempor lacinia fermentum. Integer porta, elit nec blandit porttitor, magna purus sodales diam, nec mattis arcu nisi et nisl. Pellentesque pretium, arcu sed cursus mattis, lacus purus luctus massa, quis viverra ipsum turpis eu felis. Aenean fermentum dui eu dolor tempus et vulputate tellus fermentum. Nullam est mauris, rutrum at condimentum et, fringilla nec dolor. Maecenas interdum metus vitae ligula viverra semper. Etiam et risus vel neque interdum sodales ac non tellus. 
Sed molestie aliquam accumsan. Aenean feugiat ornare risus ut dictum. Vivamus consequat pulvinar dui id convallis. Integer ut sapien quis sem mattis pulvinar. Nunc non velit dui, sit amet iaculis dui. Sed scelerisque bibendum lobortis. Maecenas dui magna, ullamcorper in posuere et, convallis non enim. Fusce elementum vestibulum ligula at malesuada. Etiam nec felis nunc, eu viverra dui. Donec elementum luctus est sed interdum. Phasellus et dignissim est. 


                
            
                  </p>
                                                    
                <br />
                  <br />
    </div>
    </div>

<!-- end #content -->

<div id="footer">
<!-- #include file="..\includes\footer.inc"-->
</div>
<!-- end #footer -->
</div>
</body>
</html>

.ASP Page End-------------------------------------------------------------

Open in new window

Include file containing Javascript-------------------------------------------------
<script>
<!-- Hide from old browsers





var TRange = null;
var dupeRange = null;
var TestRange = null;
var win = null;


var nom = navigator.appName.toLowerCase();
var agt = navigator.userAgent.toLowerCase();
var is_major   = parseInt(navigator.appVersion);
var is_minor   = parseFloat(navigator.appVersion);
var is_ie      = (agt.indexOf("msie") != -1);
var is_ie4up   = (is_ie && (is_major >= 4));
var is_not_moz = (agt.indexOf('netscape')!=-1)
var is_nav     = (nom.indexOf('netscape')!=-1);
var is_nav4    = (is_nav && (is_major == 4));
var is_mac     = (agt.indexOf("mac")!=-1);
var is_gecko   = (agt.indexOf('gecko') != -1);
var is_opera   = (agt.indexOf("opera") != -1);


//  GECKO REVISION

var is_rev=0
if (is_gecko) {
temp = agt.split("rv:")
is_rev = parseFloat(temp[1])
}


//  USE THE FOLLOWING VARIABLE TO CONFIGURE FRAMES TO SEARCH
//  (SELF OR CHILD FRAME)

//  If you want to search another frame, change from "self" to
//  the name of the target frame:
//  e.g., var frametosearch = 'main'

//var frametosearch = 'main';
var frametosearch = self;


function search(whichform, whichframe) {

//  TEST FOR IE5 FOR MAC (NO DOCUMENTATION)

if (is_ie4up && is_mac) return;

//  TEST FOR NAV 6 (NO DOCUMENTATION)

if (is_gecko && (is_rev <1)) return;

//  TEST FOR Opera (NO DOCUMENTATION)

if (is_opera) return;

//  INITIALIZATIONS FOR FIND-IN-PAGE SEARCHES

if(whichform.findthis.value!=null && whichform.findthis.value!='') {

       str = whichform.findthis.value;
       win = whichframe;
       var frameval=false;
       if(win!=self)
{

       frameval=true;  // this will enable Nav7 to search child frame
       win = parent.frames[whichframe];

}

    
}

else return;  //  i.e., no search string was entered

var strFound;

//  NAVIGATOR 4 SPECIFIC CODE

if(is_nav4 && (is_minor < 5)) {
   
  strFound=win.find(str); // case insensitive, forward search by default

//  There are 3 arguments available:
//  searchString: type string and it's the item to be searched
//  caseSensitive: boolean -- is search case sensitive?
//  backwards: boolean --should we also search backwards?
//  strFound=win.find(str, false, false) is the explicit
//  version of the above
//  The Mac version of Nav4 has wrapAround, but
//  cannot be specified in JS

 
        }

//  NAVIGATOR 7 and Mozilla rev 1+ SPECIFIC CODE (WILL NOT WORK WITH NAVIGATOR 6)

if (is_gecko && (is_rev >= 1)) {
   
    if(frameval!=false) win.focus(); // force search in specified child frame
    strFound=win.find(str, false, false, true, false, frameval, false);

//  The following statement enables reversion of focus 
//  back to the search box after each search event 
//  allowing the user to press the ENTER key instead
//  of clicking the search button to continue search.
//  Note: tends to be buggy in Mozilla as of 1.3.1
//  (see www.mozilla.org) so is excluded from users 
//  of that browser.

    if (is_not_moz)  whichform.findthis.focus();

//  There are 7 arguments available:
//  searchString: type string and it's the item to be searched
//  caseSensitive: boolean -- is search case sensitive?
//  backwards: boolean --should we also search backwards?
//  wrapAround: boolean -- should we wrap the search?
//  wholeWord: boolean: should we search only for whole words
//  searchInFrames: boolean -- should we search in frames?
//  showDialog: boolean -- should we show the Find Dialog?


}

 if (is_ie4up) {

  // EXPLORER-SPECIFIC CODE revised 5/21/03

  if (TRange!=null) {
	  
   TestRange=win.document.body.createTextRange();
 
	  

   if (dupeRange.inRange(TestRange)) {

   TRange.collapse(false);
   strFound=TRange.findText(str);
    if (strFound) {
        //the following line added by Mike and Susan Keenan, 7 June 2003
        win.document.body.scrollTop = win.document.body.scrollTop + TRange.offsetTop;
        TRange.select();
        }


   }
   
   else {

     TRange=win.document.body.createTextRange();
     TRange.collapse(false);
     strFound=TRange.findText(str);
     if (strFound) {
        //the following line added by Mike and Susan Keenan, 7 June 2003
        win.document.body.scrollTop = TRange.offsetTop;
        TRange.select();
        }



   }
  }
  
   if (TRange==null || strFound==0) {
   TRange=win.document.body.createTextRange();
   dupeRange = TRange.duplicate();
   strFound=TRange.findText(str);
    if (strFound) {
        //the following line added by Mike and Susan Keenan, 7 June 2003
        win.document.body.scrollTop = TRange.offsetTop;
        TRange.select();
        }

   
   }

 }

  if (!strFound) alert ("Search for '"+str+"' came back with no results!") // string not found

        
}
// -->
</script>

Open in new window

0
Shade22
Asked:
Shade22
  • 2
1 Solution
 
COBOLdinosaurCommented:
Just grab the text of the element you want to search:

var message = elem.innerText || elem.textContent || "";

Then search message.


Cd&
0
 
Shade22Author Commented:
How would I do that if the text I want it to search is the text under the Frequently Asked Questions "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean tincidunt mattis magna ac egestas. Maecenas mi felis, ullamcorper vel tincidunt id, bibendum quis velit. Nulla quis augue sem. Aenean porttitor mattis arcu, ut semper sapien iaculis rutrum. Maecenas sodales aliquet mollis. Nunc ac lorem vitae nibh fermentum adipiscing quis sed lorem. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Suspendisse potenti. Nam varius nunc blandit diam pharetra interdum eget quis neque.
Vivamus sed sapien et libero convallis pulvinar at id ante. Donec pharetra egestas laoreet. Nam tempor eleifend augue, a commodo metus ornare id. Nam fringilla ultricies arcu, ac placerat mi sagittis eget. Suspendisse consequat lorem enim, vitae sodales arcu. Curabitur feugiat ligula dictum tortor suscipit ut bibendum ipsum tempus. Cras vitae lacus ut ante tempor dignissim a id massa.
Quisque sagittis, nisl in venenatis laoreet, dui purus sodales lorem, vitae iaculis tellus leo eget felis. Curabitur eu arcu vel lectus commodo porta. Fusce id nulla nec ipsum vehicula scelerisque. Donec at eros odio, gravida volutpat purus. Vivamus quis nunc mauris. Duis mollis, nibh a varius euismod, sapien odio ullamcorper libero, sed ornare est felis vel libero. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Etiam feugiat vestibulum rutrum. Vivamus adipiscing dui non turpis tempus dapibus. Vivamus porta egestas lorem quis tempor. Praesent viverra felis tincidunt erat molestie sit amet eleifend justo feugiat.
Duis dapibus ipsum nec leo sagittis suscipit. Curabitur iaculis velit vitae mi scelerisque a sagittis ligula eleifend. Mauris sit amet sapien ipsum. Proin ornare laoreet odio ac commodo. Phasellus vitae nunc vel nisi sodales congue aliquet ut lectus. Proin ac nisi ac velit volutpat ultrices id vel eros. Aenean placerat, mauris a hendrerit consectetur, velit eros condimentum leo, vel aliquet felis purus cursus lacus. Donec vulputate porta dui, ac pharetra erat luctus in. Integer sapien ipsum, imperdiet ac blandit non, hendrerit sed purus. Quisque tempor lacinia fermentum. Integer porta, elit nec blandit porttitor, magna purus sodales diam, nec mattis arcu nisi et nisl. Pellentesque pretium, arcu sed cursus mattis, lacus purus luctus massa, quis viverra ipsum turpis eu felis. Aenean fermentum dui eu dolor tempus et vulputate tellus fermentum. Nullam est mauris, rutrum at condimentum et, fringilla nec dolor. Maecenas interdum metus vitae ligula viverra semper. Etiam et risus vel neque interdum sodales ac non tellus.
Sed molestie aliquam accumsan. Aenean feugiat ornare risus ut dictum. Vivamus consequat pulvinar dui id convallis. Integer ut sapien quis sem mattis pulvinar. Nunc non velit dui, sit amet iaculis dui. Sed scelerisque bibendum lobortis. Maecenas dui magna, ullamcorper in posuere et, convallis non enim. Fusce elementum vestibulum ligula at malesuada. Etiam nec felis nunc, eu viverra dui. Donec elementum luctus est sed interdum. Phasellus et dignissim est.  </p> ".
0
 
COBOLdinosaurCommented:
I have no idea what that last post is about.  So far you have not posted anything that looks like what you say you want to do.

It is very straight forward.  
You get the search term
You extract the text of the target element
You use the javascript search() to find the position of the match


Cd&
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now