Click to See Complete Forum and Search --> : How to search a select list
djstone
07-11-2003, 09:33 AM
Hi,
I have a CMS with a large select list (potentially with c500 items). The user wants to search for an individual item so that entering a successive character moves to all matching items in the select.
EG
<option>Abismal</option>
<option>Acute</option>
<option>Adorn</option>
<option>Dim</option>
At the moment, if you select the element a type 'A', you get the first item beginning with an a. Pressing 'A' again selects the second item beginning with 'A', then the third. Keying 'Ad' takes you to 'Dim'. The user wants this to take him to 'Adorn'.
I realise this will have to be done using an <input> element alongside the <select>, which will search the <select> on each onKeyUp event.
Does anyone know of any code that can do this?
Cheers,
Daniel
Khalid Ali
07-11-2003, 09:50 AM
I am thinking this might help
http://68.145.35.86/skills/javascripts/SearchValueInListBox.html
djstone
07-11-2003, 10:03 AM
Thanks Khalid,
That's a great help thanks.
Daniel
Khalid Ali
07-11-2003, 10:11 AM
:D
My pleasure Daniel
NickFeatch
05-26-2005, 10:45 AM
Hello ...
I too would like search a selection box... looks like i got a here a bit late to use the link tho :eek:
Anyways heres my code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>add an option</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
<!--
function search() {
var text=document.myform.textbox.value;
var element_count=document.myform.selectbox.size;
alert (element_count)
for (var i=0; i < element_count+1; i++) {
var select=document.myform.selectbox[i].value;
if (select==text) {
alert (select)
document.myform.selectbox[i].blur();
break;
}
}
}
//-->
</script>
</head>
<body>
<form name="myform" action="#">
<div>
<input type="text" name="textbox" /><button type="button" onclick="search();">Find Option</button>
<select name="selectbox" size=5>
<option value="apple">apple</option>
<option value="pear">pear</option>
<option value="orange">orange</option>
</select>
</div>
</form>
</body>
</html>
i've got stuck on a couple of areas
could some one please tell me how to
a) get the number of rows in the selection box
b) focus/blur (don't know the difference :confused: ) an element in the list
c) change my if equals to to if contains
gecastill
05-26-2005, 12:20 PM
Does any of you know how to create a drop-down list that allows the user to search the drop-down list using one and / or two characters, that is, if the user clicks or expands the drop-down list and then presses on two characters one after the other in the keyboard which could be the first two characters of the word they are searching for in the list.
This two characters will be save into a variable which will be compare to the first two letters of each word in the list; the first word in the list that matches to the first two characters will be selected in the list [similar to the one character search].
The one character search functionality already exist in the drop-down list so I need to add the two character search functionality to it. We are use to searching in a drop-down list using one character which matches to the first letter of a word in the list, that is, the first word in the list that has that letter.
Now, what I am looking for is to search with two characters; which will match with the first two letters of the words in the list in alphabetic order.
example:
if (wordInList.substring(0,2)==searchingString){
alert(DoThis);
}
They tell me that Firefox and Netscape 7.1+ have this feature built in.
Also I notice that WinZip has this feature built in too.
Other browsers can only search on the first letter of each option's text.
My users will be using Internet Explorer, how can I create this feature?
NOTE: I am not using Firefox, I am using Microsoft Internet Explorer 6.0.2... I need this functionality working in IE, because none of the users will be using Firefox. My users will be using Internet Explorer, how can I create this feature in IE?
The following drop-down list is working properly, but I need the search box to be part of the drop-down list. All the functionality should be in the drop-down list.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test 2</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<script type="text/javascript">
var selects = null;
function HandleKey() {
k = document.getElementById('searchbox');
keys = k.value;
if (keys.length > 0) {
FindKey(keys);
}
}
function FindKey(keys) {
// Returns a collection of objects with the specified element name.
opts = selects.getElementsByTagName('option');
for(i = 0; i < opts.length; i++) {
alert(i + ". option= " + opts.item(i).text + " searching= " + keys);
if (opts.item(i).text.substr(0, keys.length) == keys) {
// Select the option opts.item(i)
selects.selectedIndex = i;
return false;
}
}
}
function AttachSearch(id) {
// Returns a reference to the [select] element tag.
el = document.getElementById(id);
// Creates an instance of the element for the specified tag and returns a reference to the new element.
searchbox = document.createElement('input');
searchbox.id = 'searchbox';
searchbox.onkeypress = function() { setTimeout('HandleKey();', 10); };
// Returns a collection of objects with the specified element name.
selects = el.getElementsByTagName('select').item(0);
// Returns a reference to the element that is inserted into the document.
el.insertBefore(searchbox, selects);
}
window.onload = function() { AttachSearch('searchit'); }
</script>
<style type="text/css">
#searchbox
{
width: 35px;
}
</style>
<body>
<div id="searchit">
<select name="a">
<option>abb</option>
<option>abbc</option>
<option>abc</option>
<option>awahsdbg</option>
<option>baosiudgo</option>
<option>bwoaejisd</option>
<option>bzowej</option>
<option>caaijre</option>
<option>ckasdofj</option>
<option>coweruasdh</option>
<option>cxajre</option>
<option>dopriejo</option>
<option>dtaosjdfl</option>
<option>dypasj</option>
<option>eeaoijg</option>
<option>ezaosjdf</option>
<option>ezosjldkf</option>
</select>
</div>
</body>
</html>
===============================================
The following drop-down list has the look and feel I need, but the two character search in the drop-down list is not working properly.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test 3</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<script type="text/javascript">
var fnd = "";
function findOption(selects, evt) {
if (window.event) var k = String.fromCharCode(event.keyCode);
else var k = String.fromCharCode(evt.which);
if (k < " " || k > "~") return true;
if (k == " ") fnd = "";
else fnd += k.toUpperCase();
for (var i = 0; i < selects.options.length; i++) {
var optText = selects.options[i].text.toUpperCase();
alert(i + ". option= " + optText + " searching= " + fnd);
if (optText.substr(0, fnd.length) == fnd) {
selects.selectedIndex = i;
return false;
}
}
//if (fnd.length > 2) fnd = "";
//return false;
}
</script>
<body>
Test Drop-Down menu
<div id="searchit">
<select onkeypress="return findOption(this)">>
<option value ="0">AB AT HOME</option>
<option value ="1">AC AT HOME</option>
<option value ="2">AD AT HOME</option>
<option value ="3">BILLY</option>
<option value ="4">BRAND</option>
<option value ="5">BURRO</option>
<option value ="6">DARK</option>
<option value ="7">DESIGNER</option>
<option value ="8">DICOR</option>
<option value ="9">EDUCATION</option>
<option value ="10">ENTERTAINMENT</option>
<option value ="11" selected>GENERIC</option>
<option value ="12">LILLY PULITZER</option>
<option value ="13">MARQUIS</option>
<option value ="14">OPEN LINE</option>
<option value ="15">PROPRIETARY</option>
<option value ="16">SAPER 300</option>
<option value ="17">SPORTS</option>
<option value ="18">SUPREME DIMENSIONS</option>
<option value ="19">TEST</option>
</select>
</div>
</body>
</html>
Any hints, suggestions, or ideas on how to do this with JavaScript?
If you have any information or suggestions regarding this topic, please post a reply here.
NickFeatch
05-27-2005, 05:42 AM
well that helped me :D
Thanks