Current location: Hot Scripts Forums » General Web Coding » JavaScript » Can JavaScript do Incremental Searching?


Can JavaScript do Incremental Searching?

Reply
  #1 (permalink)  
Old 01-22-04, 02:46 AM
trigger_ph trigger_ph is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Red face Can JavaScript do Incremental Searching?

Is it possible to implement incremental searching using JavaScript?
For instance, I have a textbox and a select element within a form. When I type "f" in the textbox, an option matching "f" as a first letter in the said select element becomes selected. If I further type "o" what becomes selected in the select element are those options having "fo" as first two letters, and so on. When I type in "food" a match, if found, is selected, or whichever is nearer.

I hope someone can enlighten me on this. Thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-09-04, 08:40 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
yes, that can be done in several ways.

I'd start by creating a loop which can be used to loop through all options in the selectbox.

Then I'd grab the input from the textfield and use a Regular Expression to see if the beginning of the current option-text matches the textbox-string.

If so, break the loop and change the selectedId property of the selectbox.
Otherwise, just move to the next option on the list without changing which option is selected.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 12-28-04, 02:10 AM
niranjan niranjan is offline
New Member
 
Join Date: Dec 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Your Soln to Incremental searching

Just copy this code and save in html file .........run it and enjoy!!!

<html>
<head>
<title>Dropdown Filter</title>
</head>

<body>
<script language="JavaScript" type="text/javascript">
<!--


/*
filtery(pattern, list)
pattern: a string of zero or more characters by which to filter the list
list: reference to a form object of type, select

Example:
<form name="yourForm">
<input type="text" name="yourTextField" onchange="filtery(this.value,this.form.yourSelect) ">
<select name="yourSelect">
<option></option>
<option value="Australia">Australia</option>
.......
*/
function filtery(pattern, list){


/*
if the dropdown list passed in hasn't
already been backed up, we'll do that now
*/
//alert("list.bak"+list.bak);
if (!list.bak){
/*
We're going to attach an array to the select object
where we'll keep a backup of the original dropdown list
*/
list.bak = new Array();
for (n=0;n<list.length;n++){
list.bak[list.bak.length] = new Array(list[n].value, list[n].text);
}
}

/*
We're going to iterate through the backed up dropdown
list. If an item matches, it is added to the list of
matches. If not, then it is added to the list of non matches.
*/
match = new Array();
nomatch = new Array();
for (n=0;n<list.bak.length;n++){
if(list.bak[n][1].toLowerCase().indexOf(pattern.toLowerCase())!=-1){
match[match.length] = new Array(list.bak[n][0], list.bak[n][1]);
}else{
nomatch[nomatch.length] = new Array(list.bak[n][0], list.bak[n][1]);
}
}

/*
Now we completely rewrite the dropdown list.
First we write in the matches, then we write
in the non matches
*/
for (n=0;n<match.length;n++){
list[n].value = match[n][0];
list[n].text = match[n][1];
}
for (n=0;n<nomatch.length;n++){
list[n+match.length].value = nomatch[n][0];
list[n+match.length].text = nomatch[n][1];
}

/*
Finally, we make the 1st item selected - this
makes sure that the matching options are
immediately apparent
*/
list.selectedIndex=0;
}
// -->
</script>

<form name="yourForm">
Search
<!--<input type="text" name="yourTextField" onkeyup="filtery(this.value,this.form.yourSelect)" onchange="filtery(this.value,this.form.yourSelect) "> -->
<input type="text" name="yourTextField" onkeyup="filtery(this.value,document.yourForm.your Select)" onchange="filtery(this.value,this.form.yourSelect) ">

<select name="yourSelect">
<option></option>
<option value="Australia">Australia</option>
<option value="China">China</option>
<option value="England">England</option>
<option value="New Zealand">New Zealand</option>
</select>
</form>


</body>
</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 12-29-04, 03:19 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
I'm not a moderator, but here's a tip: Use the [code ][/code ] wrapper when posting code longer than a few lines, it looks better and it keeps the forum from converting the code to smilies etc.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript menu covered by java applet shaisachs JavaScript 7 12-29-04 12:38 AM
searching for Domain Reseller API script aldalil Script Requests 4 12-18-03 08:37 PM
Reaaly stuck about javascript over frames muratisik JavaScript 1 12-14-03 12:58 PM
php and javascript together? gamextremer2003 PHP 5 11-06-03 03:18 PM
Forcing a JavaScript to abort HiMyNameIs JavaScript 3 09-18-03 11:23 AM


All times are GMT -5. The time now is 01:28 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.