Spotlight-Like Search As You Type With CSS, AJAX & JS
OS X has a lot of great effects. These are great building blocks for creating effects for the web. You might have seen my previous tutorial on creating a Dashboard-Like effect using JS and CSS. This tutorial will show you how to create a Spotlight-Like effect for your search boxes. Take a look at the picture to the right to see where we are headed:
You can preview the finished effect here. This example will search through 3 text files of lists of names of celebrities, athletes, and presidents. In most real world situations, you would be searching inside databases, but for this preview I used text files. I have also zipped up the necessary files and put them here. This zip contains all required images, CSS files, HTML files, and javascript files.

What we need to do
- Create the HTML markup
- Style a bit with CSS
- Write our Javascript code
- Write the PHP code to return the search results
- Finish up the CSS
- Tie everything together
I should note here that we are going to use PHP, but any scripting language will do.
Create the HTML markup
We’re first going to start by laying out our HTML. This is a really simple step. Inside your head tag, put:
1 2 3 4 | <script src="scriptaculous/lib/prototype.js" type="text/javascript"></script> <script src="scriptaculous/src/scriptaculous.js?load=effects" type="text/javascript"></script> <script src="search.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="search.css" media="screen" /> |
You might notice that we are using script.aculo.us. We are including those libraries so we can get a few nice animation effects. If you do no want these effects (more info in the JS section), you don’t need to include lines 1 and 2. Note: On line 2, the source also specifies to only load the effects.js file.
Line 3 points to our javascript file. Line 4 points to our CSS file. Simple right? Next place the following code somewhere inside your body tag:
1 2 3 4 5 6 7 | <form id="search_form" action="" method="get"> <input type="text" value="" name="q" id="q" autocomplete="off" onkeyup="doSearch();" /> <input type="image" id="search_image" onclick="doSearch();return false;" src="search_icon.png" alt="Search" /> </form> <div id="searchResultsDiv"> Start typing. Your results will show below instantly. </div> |
This is the code that does a few things. It creates a search box, q, which has an event handler, onkeyup, that calls our doSearch() method. We then create an image that has the onclick method set to the same action. We need to return false here so that the form’s action is not called.
The next div is where the search results will come back. Right now I have some text in there that tells people to just type and see results. You could have also left it blank if you don’t want there to be anything before users type.
That’s it for our HTML. Very, very, very simple. Let’s move onto the CSS so we can style this HTML nicely.
Style a bit with CSS
I’ve broken the discussion of the CSS file up into two parts. We will talk about the second portion in just a bit. The full CSS file is available in the zip file. In this first part, we are just going to style the HTML we’ve written so far. Not any of the search results. This section contains only 3 things:
1 2 3 4 5 6 7 8 9 10 | a img { border: 0px; } form#search_form input { vertical-align:middle; display:inline; } form#search_form #search_image { margin-left: -15px; } |
Like our HTML, this is very simple. The a img just makes sure we don’t draw a border around images inside links. Next, we make sure all of our form elements are lined up. We also make the search icon have a left margin of -15 pixels. This will make our search icon overlap with the text box.
We’re all done with the CSS for now. Let’s move onto the Javascript.
Write our Javascript code
Let’s analyze our JS code a bit. This first part creates a typical XMLHttpRequest and creates a few helper methods for it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | function createRequestObject() { var ro; var browser = navigator.appName; if(browser == "Microsoft Internet Explorer"){ ro = new ActiveXObject("Microsoft.XMLHTTP"); }else{ ro = new XMLHttpRequest(); } return ro; } var http = createRequestObject(); function sndReq(action) { http.open('get', action); http.onreadystatechange = handleResponse; http.send(null); } function handleResponse() { if(http.readyState == 4){ document.getElementById('searchResultsDiv').innerHTML = http.responseText; } } |
I’m not going to spend time explaining the workings of these AJAX objects. There are tutorials better at that. I’ll just say that we will use the method sndReq to send asynchronous requests with action as the URL we want to request. The function handleResponse will be called when the AJAX request is done. When we receive the finished request, we set the innerHTML of our search results div with the response text. Next we have our doSearch and openWindow functions:
1 2 3 4 5 6 7 8 | function doSearch() { var val = 'do_search.php?q=' + document.getElementById('q').value; sndReq(val); return false; } function openWindow(url) { window.open(url,'popupWindow','resizable=no, scrollbars=yes, toolbar=no, status=no, height=650, width=720'); } |
doSearch() will send an AJAX request to do_search.php with the q variable set to the search box’s value. openWindow() will simply open a new window and go to a certain URL. This code is executed when an item from the search results is clicked. You can change this function to do whatever you wish when your search results are clicked. Finally, we have the following javascript code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | function expandOrClose(arrow_id, list_num, list_name) { var a = document.getElementById(arrow_id); if(a.name=='d') { a.src='arrowRight.gif'; a.name='r'; hideSearchTypes(list_num,list_name); } else { a.src='arrowDown.gif'; a.name='d'; showSearchTypes(list_num,list_name); } return false; } function hideSearchTypes (i,j) { var theDiv = document.getElementById(j).getElementsByTagName('li'); for(var d = 1; d < theDiv.length; d++) { var theID = 'res-'+i+'-'+d; Effect.DropOut(theID); } } function showSearchTypes (i,j) { var theDiv = document.getElementById(j).getElementsByTagName('li'); for(var d = 1; d < theDiv.length; d++) { var theID = 'res-'+i+'-'+d; Effect.Appear(theID); } } |
expandOrClose() is called when a user clicks on a category heading (the blue sections in the image above). What we want to do is compress that section so it takes up no space. Our JS code simply changes the arrow direction and then calls one of the next two functions. The two functions go through each remaining row in that category and hide/show it. These two functions use the script.aculo.us effects. You could also just do something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function hideSearchTypes (i,j) { var theDiv = document.getElementById(j).getElementsByTagName('li'); for(var d = 1; d < theDiv.length; d++) { var theID = 'res-'+i+'-'+d; document.getElementById(theID).style.display = 'none'; } } function showSearchTypes (i,j) { var theDiv = document.getElementById(j).getElementsByTagName('li'); for(var d = 1; d < theDiv.length; d++) { var theID = 'res-'+i+'-'+d; document.getElementById(theID).style.display = ''; } } |
That code immediately hides or shows each row. It also does not require the two script.aculo.us libraries.
That looks about it for the javascript code. Let’s move onto our PHP code.
Write the PHP code to return the search results
I want to repeat that this does not require PHP. Any language will do. Perl, ASP, Python, etc. I use PHP, so that is what this tutorial will use, but the rest of the code is independent of what language you use.
We’ll start with the first bit of PHP code:
1 2 3 4 | <?php //include any permission checks $q = $_GET['q']; ?> |
This will set the variable $q to the value from our request. Be sure to make any proper permission checks here. For instance, you may want to confirm that the user has logged in or escape the strings to prevent any hacking.
I am going to start out with what your PHP should print out. Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <div id="search_results"> <ul id="first_list" class="search_results_list"> <li class="searchHeader"> <a href='' onclick="expandOrClose('arrow1',1,'first_list'); return false;" ><img id="arrow1" src="arrowDown.gif" alt="expand or close" name="d" /> People</a> </li> <li id="res-1-1" class="searchResultRow"> <a href="" onclick="openWindow('http://apple.com');return false">Steve Jobs</a> </li> <li id="res-1-2" class="searchResultRow"> <a href="" onclick="openWindow('http://microsoft.com');return false">Bill Gates</a> </li> </ul> </div> |
Surround your output in a div with id as search_results. Each category of results you make will be its own ul. Give it a unique ID and make its class search_results_list. Each ul should have one li with class=”searchHeader”. Inside that li, we make a link, which calls our javascript function expandOrClose(). We put the arrow image, and then we write the title of the category, in this case People.
The following li’s should be of class searchResultRow. Their id should take the form of res-#-#. The first number is the category number. The second is the row number in that category. Each category begins with row 1. Inside these li’s you can print out your results. I surround them in a link that opens a window pointing to their respective companies.
The process of writing a searching algorithm is very complex and requires a tutorial in itself. I still want to show you some examples of how to write one. This first one is just pseudocode, but it describes the process of writing a search for our search box.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | get inputed search string sql code that searches for that string in important column 1 find how many results there were if results == 0 print out nothing for this category else print out the UL for this category loop through each result print out LI for this row close UL Repeat for each category you wish #Finally Check if nothing was ever printed out If nothing printed Print out no results |
This pseudo-code should be pretty self explanatory. The following code is the actual php code I use for the example linked above. It searches through text files matching the input string to parts of people’s names. Many of you will want this to be written so it can query a MySQL database. The transformation of this code to MySQL code is not very difficult.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | <div id="search_results"> <?php $count = 0; $celebs = file('celebs.txt'); $l = 1; foreach ($celebs as $celeb) { $splits = split(" ",$celeb); $firstName = $splits[0]; $lastName = $splits[1]; $possibleName = ""; $possibleName = $splits[2]; if( preg_match("/^$q/i",$firstName." ".$possibleName) !=0 || preg_match("/^$q/",$lastName." ".$possibleName) !=0 || preg_match("/^$q/",$possibleName) !=0 || preg_match("/^$q/i",$firstName." ".$lastName." ".$possibleName) !=0 ) { if($l == 1) { ?> <ul id="celeb_list" class="search_results_list"> <li class="searchHeader"> <a href='' onclick="expandOrClose('arrow1',1,'celeb_list'); return false;"><img id="arrow1" src="arrowDown.gif" alt="expand or close" name="d" /> Celebrities</a> </li> <?php } ?> <li id="res-1-<?php echo $l; ?>" class="searchResultRow"> <?php echo $firstName . " " . $lastName . " " . $possibleName; ?> </li> <?php $count++; $l++; } } if($l != 1) { ?></ul><?php } $athletes = file('athletes.txt'); $l = 1; foreach ($athletes as $athlete) { $splits = split(" ",$athlete); $firstName = $splits[0]; $lastName = $splits[1]; $possibleName = ""; $possibleName = $splits[2]; if( preg_match("/^$q/i",$firstName." ".$possibleName) !=0 || preg_match("/^$q/",$lastName." ".$possibleName) !=0 || preg_match("/^$q/",$possibleName) !=0 || preg_match("/^$q/i",$firstName." ".$lastName." ".$possibleName) !=0 ) { if($l == 1) { ?> <ul id="athlete_list" class="search_results_list"> <li class="searchHeader"> <a href='' onclick="expandOrClose('arrow2',2,'athlete_list'); return false;"><img id="arrow2" src="arrowDown.gif" alt="expand or close" name="d" /> Athletes</a> </li> <?php } ?> <li id="res-2-<?php echo $l; ?>" class="searchResultRow"> <?php echo $firstName . " " . $lastName . " " . $possibleName; ?> </li> <?php $count++; $l++; } } if($l != 1) { ?></ul><?php } $presidents = file('presidents.txt'); $l = 1; foreach ($presidents as $president) { $splits = split(" ",$president); $firstName = $splits[0]; $lastName = $splits[1]; $possibleName = ""; $possibleName = $splits[2]; if( preg_match("/^$q/i",$firstName." ".$possibleName) !=0 || preg_match("/^$q/",$lastName." ".$possibleName) !=0 || preg_match("/^$q/",$possibleName) !=0 || preg_match("/^$q/i",$firstName." ".$lastName." ".$possibleName) !=0 ) { if($l == 1) { ?> <ul id="president_list" class="search_results_list"> <li class="searchHeader"> <a href='' onclick="expandOrClose('arrow3',3,'president_list'); return false;"><img id="arrow3" src="arrowDown.gif" alt="expand or close" name="d" /> Presidents</a> </li> <?php } ?> <li id="res-3-<?php echo $l; ?>" class="searchResultRow"> <?php echo $firstName . " " . $lastName . " " . $possibleName; ?> </li> <?php $count++; $l++; } } if($l != 1) { ?></ul><?php } if($count==0) { ?> <ul id="first_list" class="search_results_list"> <li class="searchHeader"> <a href='' onclick="expandOrClose('arrow1',1,'first_list'); return false;"><img id="arrow1" src="arrowDown.gif" alt="expand or close" name="d" /> Results</a> </li> <li id="res-1-1" class="searchResultRow"> No Results Found </li> </ul> <?php } ?> </div> |
If you know PHP, this should make sense. I basically loop through each file array and pattern match the inputed string to the first name, the last name and then the first name and the last name. You might notice the variable $possibleName. That is there if the person might have a middle name included. If the pattern matching returns true, we go inside and if it’s the first time in that category we print out the header. Then we print out the specific result, the person’s name that matched. We loop through that 3 times (once for each data file). At the end we do a special UL print out if no results were found. Hopefully, for those of you who understand PHP, that example will help you in writing your own search. You will need to spend some time writing this part, and unfortunately, I can’t help too much considering it’s based heavliy on which language you use and how your database is structured. If you need help on a specific implementation, please leave a comment here, and I will attemp to help out.
Moving on. After you’ve finished your PHP (or other language) let’s return to the CSS code for the search results.
Finish up the CSS
Let’s get right in:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #search_results { margin: -18px 0 0 -38px; width: 200px; font-family: 'Lucida Grande', Verdana, Arial, Sans-Serif; font-size: 13px; } ul.search_results_list { margin-bottom: 0px; margin-top: -1px; } ul.search_results_list li { list-style: none; display: block; padding: 2px; } li.searchHeader { background-color:#6699FF; color:#FFFFFF; border:#CCCCCC 1px solid; font-weight: bold; font-size: 15px; } li.searchHeader a { text-decoration:none; color:#FFFFFF; } li.searchHeader a:hover { text-decoration:none; color:#000000; } li.searchResultRow { background-color:#FFFFFF; color:#333333; border-left:#CCCCCC 1px solid; border-right:#CCCCCC 1px solid; border-bottom:#CCCCCC 1px solid; } li.searchResultRow a { text-decoration: none; color: #111; margin-left: 12px; } li.searchResultRow a:hover { text-decoration: underline; } |
There’s nothing out of the ordinary in this CSS. It just sets up all the colors and borders we want.
Tie everything together
It looks like we have some really good working code. There’s nothing else to do. Just give it a run and check it out. It’s pretty cool. We now have search as you type implemented completely in very little code using HTML, CSS, Javascript, AJAX and some PHP. Spotlight has now officially reached the web.
There’s not much else to do. Just put your code on your site and let your users enjoy Spotlight-Like searching right within your website. All code here is Public Domain. If you use it on your site, it’d be nice if you posted a comment here linking to your site, so I can see how people are using it. I love getting feedback, so if you have any comments, questions, examples, or complaints, please post a comment.
If you liked this tutorial, check out my tutorial on Dashboard-Like effects in CSS and also my tutorial on Flickr-like edit in place.
Featured Info
Through wireless internet services people are at ease of learning online e.g. they can do professional certifications like Cisco certifications 640-861 or they can learn through various tutorials. Businesses that require frequent communication across the country can make use of voip on their already installed data networks for cost effective communication. With the evolution of internet, many webhosting companies have emerged providing cheap web hosting plan offer free search engine placement and also provide opportunities to generate revenue through ppc affiliate program.
Advertisement
Are you searchingfor a old friend or relative that you lost track of through the years? If so you may be able to find them through an online people search. A people search website will let you find people online by searching through a massive database of public records such as marriage records, criminal records, and property records. A people search report serves as a great background check for babysitters and house keepers!
42 Replies
Craig on 4/15/2007 at 18:12awesome idea, i think google did somethink like this in there labs
luke dorny on 4/16/2007 at 19:55How nice to roll up everything involved into a tute.
This will be linked to for some time to come, i’m sure!Good work!
Ian on 4/20/2007 at 10:05Impressive.
Michael on 4/20/2007 at 14:16Nice effect! This would be very useful in so many different web apps.
Just wondering why you didn’t use prototype’s Ajax.Request method rather than creating your own request object? Prototype does a nice job of hiding the differences between browsers while letting you keep your JS clean.
13.vc » Blog Archive » Spotlight-Like Search As You Type w/ CSS,AJAX & JS on 4/20/2007 at 14:17[...] CSS. This tutorial will show you how to create a Spotlight-Like effect for your search boxes.”read more | digg [...]
MezZzeR on 4/20/2007 at 14:25It’s a lot easier to iterate through the result set from MySQL if you return JSON using json_encode (php5+). If you keep outputting raw HTML in your fetch script which listens for the xhr request, then what if your layout changes? You’d have to go through each script and change it. It’s best to just retrieve it as raw data either JSON or XML. I would prefer JSON–it’s lightweight and fast vs xml.
All you would have to do in your handler function is go:
var data = eval(o.responseText);
James on 4/20/2007 at 14:42MezZzeR: could you possibly explain how to integrate with Prototype and JSON a little more? I’m new to all this :)
Celebrity PWN on 4/20/2007 at 15:02Very cool script. The only thing its missing is being able to use the arrow down/up keys. Otherwise, its better than most.
Jonathon on 4/20/2007 at 15:17Ok, I’m going to be a code nazi here. But do you not recognize that you are doing the exact same search for all three data files? Why not have an outer loop that containing an array with your three data files to search, then just apply the same code to all of them? That way, if you want to make a change to your algorithm you only have to make it in one place. Also, if you wanted to add another datafile, it would be as simple as adding it to the array of files to search.
Now that I’ve got my complaining out, great article! Good explanation of how the different aspects work together.
One other though. If you are already using script.aculo.us for your effects, why not one of the many frameworks for handling AJAX requests/responses? They will generally be developed for cross browser support and well tested.
Dustin Bachrach on 4/20/2007 at 15:21To all: Great points, and thanks for all the nice comments.
Michael, I did not even know that existed. All the tutorials I read had used the same code I used. Thanks.
Jonathon: You make a really good point on the PHP. I was first going to search the data files slightly differently in the beggining, but then I decided against it. I probably should have made the outerloop. This is more as an example, and I don’t see reading from a text file as being the primary use of this code, so I don’t think how I coded it will have an effect on other’s, but your point is correct.
If I get some time, I’m going to go back and rewrite some of this code using the Ajax.request and also clean up the PHP.
Thanks to all for your comments. Keep ‘em coming.
sam on 4/20/2007 at 15:45Nifty search.
one warning: this has the potential to hammer your database - especially if you are using %search% search% won’t be AS bad. But for each letter that is typed a query is being submitted to the db which can be a drain if you expect your site to have a fair amount of traffic. A compromise might be to put a delay in your dosearch() for a second or 2 - so they can type a few letters first - if javascript can do something like that. All I’ve found issetTimeout(”dosearch()”,1250);
Dan on 4/20/2007 at 16:36Cool thing. I like those Ajax based dynamic searches. Have you seen this search-as-you-type Google Search?
Budlight on 4/20/2007 at 18:02I think this would be cool with the dynamic google search. Really cool with the categories though.
windwaker on 4/20/2007 at 18:25This is awesome; is there any license on this, or can we do what we want with it?
Bluesix on 4/20/2007 at 19:20Ahh… if you’re using the prototype library, you don’t need to write any of the XMLHttp stuff. Go check out the prototype documentation for the Ajax.Updater or Ajax.Request methods. The lib also makes it easy to use “loading…” images using properties like Element.show() / hide()
Dustin Bachrach on 4/20/2007 at 23:04Sam: This is a very important point. If you are going to be querying your database in this search, it will be a HUGE performance hammer. Please be wary of this. Make sure you can do as much query optimization on this, and also do use timeouts to prevent your database from frying. Very good note.
Windwaker, all the code is public domain, so please feel free to use it any projects, commercial or personal. I get so much help from the community for free, so I always release my tutorial code as Public Domain. It’s the least I can do in return. If you don’t mind, please come back and leave a comment linking to where you used it. I would love to see how people implement this code. Thanks.
Bluesix: I’m going to give the Prototype documentation a full lookthrough. From what you said, that seems like making AJAX-enabled sites even easier. Thanks for the tip.
Because Its Possible.com » Blog Archives » links for 2007-04-21 on 4/20/2007 at 23:33[...] Spotlight-Like Search As You Type With CSS, AJAX & JS » Dustin Bachrach Blog (tags: ajax code howto javascript php programming search tutorial tutorials css) [...]
tinkertim on 4/20/2007 at 23:41I’ve been wanting to get more into Ajax but all I manage to find publishing freely are JS/Java developers arguing about how it should be done. I really like this tutorial because I understood everything right away, going to have fun checking out some of the rest of the stuff you make available. Thanks for making something useful and free without a ton of ads in everyone’s face, some of us really appreciate that ;)
links for 2007-04-21 « toonz on 4/21/2007 at 17:24[...] Spotlight-Like Search As You Type With CSS, AJAX & JS » Dustin Bachrach Blog (tags: ajax css javascript search) [...]
windwaker on 4/22/2007 at 00:00Awesome. Looks like I’ll be reading this blog on a regular basis, based on how much this has helped me. :)
Yuzle on 4/22/2007 at 15:00The J in AJAX stands for JavaScript. Hurray for titles with redundant information!
mpan3 on 4/23/2007 at 18:33Nice, Very easy for beginners to understand. I have implemented your algorithm here : http://mpan3.homeip.net/
mpan3 on 4/23/2007 at 20:56Dustin, one problem i seems to be having is the fact that every key-stroke generates a http request, is there an easy to get around that?
Dustin Bachrach on 4/23/2007 at 20:59mpan3: Yes this script will flood your server with requests. Sam above said this:
“A compromise might be to put a delay in your dosearch() for a second or 2 - so they can type a few letters first - if javascript can do something like that. All I’ve found is
setTimeout(”dosearch()”,1250);”
That looks like a good suggestion. Let me know how it goes.
mpan3 on 4/23/2007 at 22:41Dustin, if i am not mistaken, sam’s method stills sends the exact same amount of request, just each delayed by 1.25sec each. I am currently trying to get the script to send in a request ONLY once every 2 seconds or so, or when the user forces the submit button. I’ll keep you posted.
Dustin Bachrach on 4/24/2007 at 06:43Oh, I see what you mean. I think you are correct in that it will send each request. I wonder if you could have a global timer variable. And inside the doSearch() method check if that global has been fired in the last 2 seconds. If not, then send the request and fire the timer. Does that make sense?
I hope that might work. Post when you find something that works. I am very interested in a better solution.
mpan3 on 4/25/2007 at 00:45sadly, after much effort. I’ve resorted back to the old press-enter-to-submit method of searching, firefox is just too buggy with http requests(http://www.quirksmode.org/blog/archives/2005/09/xmlhttp_notes_a_1.html) and there doesn’t seems to be a way to do an idle-check with the client JS.
anyways, here it is again:
http://mpan3.homeip.net/the default behavior is every 4 letters are queried, and when the user presses enter, the entire string is queried. now, i just need to optimize the search code :D
Live Search As You Type and Live Sorting Tables With JS » Dustin Bachrach Blog on 5/7/2007 at 21:21[...] iTunes live search as you type? Well, this is a clone. You might have seen something like this in a previous tutorial I wrote about Spotlight-like search. What we have in this sample code is similar to that in the [...]
Redwall_hp » 29 CSS and AJAX Resources on 5/18/2007 at 14:39[...] Spotlight-like Search-As-You-Type [...]
roger on 5/27/2007 at 09:37Great job.
Is it possible to make the search results hyperlinks, and clickable, so they could take me to my search result page?
Pat on 6/11/2007 at 13:15I have to agree! Great job.
But is it at all possible to make the search results hyperlinks, and clickable?
Dustin Bachrach on 6/11/2007 at 13:31In response to doing the hyperlinks:
Me example of the script does not do hyperlinks, but it is very easy to add hyperlinks. If you look at the bit of HTML code write above where I show the PHP code it shows an example of what your php script should generate. In this HTML code it includes the tag wrapped around the contents of your li element. This is exactly what you want to do. So to add hyperlinks just adjust your PHP script slightly to how it returns the HTML. Just have it print out anchor tags like you would any list.
I hope this helps.
links for 2007-07-18 | giancarlo.dimassa.net on 7/17/2007 at 18:58[...] Spotlight-Like Search As You Type With CSS, AJAX & JS » Dustin Bachrach Blog (tags: css javascript ajax search development example scriptaculous) [...]
Links Web on 9/30/2007 at 14:27[...] http://www.dbachrach.com/blog/2007/04/15/spotlight-like-search-as-you-type-with-css-ajax-js/ [...]
Marc on 10/12/2007 at 06:50In response to doing the hyperlinks:
Me example of the script does not do hyperlinks, but it is very easy to add hyperlinks. If you look at the bit of HTML code write above where I show the PHP code it shows an example of what your php script should generate. In this HTML code it includes the tag wrapped around the contents of your li element. This is exactly what you want to do. So to add hyperlinks just adjust your PHP script slightly to how it returns the HTML. Just have it print out anchor tags like you would any list.
****I am a bit of a noob to PHP , how exactly would you add hyperlink to your search results?? thank you
Andrew Baerg on 10/16/2007 at 15:48In response to finding a way to limit the number of searches sent to the server… you can set a global var which handles the setTimeout and use clearTimeout to prevent the search from getting sent if the user is still typing. So for each keypress, a function gets called which
1. clears the current timeout — clearTimeout(timeout);
2. sets a new timeout (i have found a 500 ms timeout is about right) — timeout = setTimeout(”sndReq()”, 500);the effect is that whenever the use pauses, the search is performed.
thanks for the great tutorial!
Design Pilez » Spotlight-Like Search As You Type w/ CSS,AJAX & JS on 11/3/2007 at 11:15[...] read more | digg story [...]
Top 100 AJAX 'form' related scripts for 2007 | Nobox Media on 11/18/2007 at 03:02[...] Spotlight Like Search [...]
Webmaster 38 » Blog Archive » Spotlight Like Search at ajax scripts compound on 1/3/2008 at 19:42[...] Spotlight Like Search [...]
matt on 1/29/2008 at 12:47good works…. perfect.
Casey on 1/30/2008 at 14:54when I put this on a test page it pushed down the entire page every time I enter something in the search box. Is there any easy way to make this float over the rest of the page… or is it floating already and I’m missing some css or javascript to do that??
Cameron on 7/17/2008 at 16:13Is there an easy way to be able to click the recommend research result and have it appear in the search box. I was hopping to do this with javascript or something, rather then creating a link and URL string. Any ideas?



