/    Sign up×
Community /Pin to ProfileBookmark

How can I search a drop-down list using JavaScript in Microsoft Internet Explorer.

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 = “”;
return false;
} else fnd += k.toUpperCase();

if (fnd.length <= 2) {
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;
//if (fnd.length == 2) fnd = “”;
return false;
} // end of inner-if body
} // end of for loop
} else {
if (optText.substr(fnd.length-1, fnd.length) == fnd) {
selects.selectedIndex = i;
return false;
}
}
} // End of the findOption() function.
</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.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51May 31.2005 — On [URL=http://www.codingforums.com]Coding Forums[/URL]

, under the post a Javascript forum, there is a script that does this. I would do it, but some reason my work blocks that forum!

Eric
Copy linkTweet thisAlerts:
@sparqMay 31.2005 — On [URL=http://www.codingforums.com]Coding Forums[/URL]

, under the post a Javascript forum, there is a script that does this. I would do it, but some reason my work blocks that forum!

Eric[/QUOTE]

http://www.redime.com/cgi-bin/nph-testing.cgi
Copy linkTweet thisAlerts:
@DiyariJun 09.2005 — http://www.redime.com/cgi-bin/nph-testing.cgi[/QUOTE]

Sparq, can you please repost the javascript i am not able to access this link http://www.redime.com/cgi-bin/nph-testing.cgi

thanks
Copy linkTweet thisAlerts:
@sparqJun 10.2005 — I didn't post a javascript, I was replying to Alien about his work blocking that site... I posted a link to a free proxy service that is available to most people. Thanks
×

Success!

Help @gecastill spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...