Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > help with selecting items in a list box

Reply
Thread Tools

help with selecting items in a list box

 
 
newcomer
Guest
Posts: n/a
 
      11-24-2004
I have a javascript index that is similar to the one in the Windows
help. It has a text field that allows the you to type text and it
finds the closest item in the list below the text field. The list is
simply a select element with options that looks like this:

<SELECT>
<OPTION VALUE="1">A
<OPTION VALUE="2">B
<OPTION VALUE="3">C
</SELECT>

My problem is when I have a large list, the closest entry selects the
item in the list at the bottom of the list box, not the top of the
list box.

So here's the question: Is there a way to get the list box to display
the selected item and move it to the top of the list and not just find
it at the bottom?

To get an idea of what I 'want' it to look like, in Windows, go to
Help & Support (in XP it's in your start menu) and pick Index. I
basically want it to mirror that functionality.
 
Reply With Quote
 
 
 
 
Yann-Erwan Perio
Guest
Posts: n/a
 
      11-24-2004
newcomer wrote:

> I have a javascript index that is similar to the one in the Windows
> help. It has a text field that allows the you to type text and it
> finds the closest item in the list below the text field.


> So here's the question: Is there a way to get the list box to display
> the selected item and move it to the top of the list and not just find
> it at the bottom?



<form action="foo" onsubmit="return false;">
<script type="text/javascript">
document.write(
"<input type='text' name='search' onkeyup='S(this)'>"
);
</script>
<select name="list" size="3">
<option value="Batou" selected="selected">Batou</option>
<option value="Bormer">Bormer</option>
<option value="Ishikawa">Ishikawa</option>
<option value="Major">Major</option>
<option value="Pazu">Pazu</option>
<option value="Tachikoma">Tachikoma</option>
<option value="Togusa">Togusa</option>
</select>
</form>

<script type="text/javascript">
function S(oTxt){
var oSel, size, text, value;

oSel = oTxt.form.elements["list"];
size = oSel.size;
text = oTxt.value.toLowerCase();

for(var ii=0, opt=oSel.options, n=opt.length; ii<n; ii++) {
value = opt[ii].value.toLowerCase();
if(value.indexOf(text)==0) {
opt[ii+size<n?ii+size:n-1].selected=true;
opt[ii].selected=true;
break;
}
}
}
</script>
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help selecting items from a list box trigger by what selected in dropdown list mldardy ASP .Net 1 12-10-2008 02:24 AM
Is there any way to append some items to List box, without retrieving all items through AJAX? Anjan Bhowmik ASP .Net 1 02-14-2008 09:02 PM
Selecting items in a list/menu with javascript Brian Javascript 4 11-19-2005 03:19 AM
Accessing List Items that were added to a list box via JavaScript Scott324 ASP .Net 3 10-27-2005 09:17 PM
Selecting items in a list box Mark Javascript 0 10-31-2003 07:41 AM



Advertisments