Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Highlight option in a multiple list box

Status
Not open for further replies.

silud

MIS
Feb 26, 2003
7
GB
I am using PHP to query a database and populate a multiple SELECT box. It also highlights the selected options. Since the list box is only two lines high, and there are more than two options, sometimes the selected items do not show in the list box. If you scroll down, they are highlighted as you would expect. How do you "pre-scroll" to the selected options, to ensure that the highlighted options are visible on page load?

I hope this makes sense!
 
You would have to do this client side...

<HTML>
<HEAD>
<SCRIPT language=&quot;JavaScript1.2&quot;>
function showSelected() {
document.myForm.myList.focus();
select_option(document.myForm.myList, 5);
}
function select_option( selectbox, index )
{
for( var i = 0; i < selectbox.options.length; i++ )
{
if( i == index )
selectbox.options.selected = true;
else
selectbox.options.selected = false;
}
}
</SCRIPT>

</HEAD>
<BODY onLoad=&quot;showSelected();&quot;>
<FORM name=&quot;myForm&quot;>
<SELECT name=&quot;myList&quot; MULTIPLE>
<OPTION value=&quot;foobar&quot;>foobar</OPTION>
<OPTION value=&quot;foobar1&quot;>foobar1</OPTION>
<OPTION value=&quot;foobar2&quot;>foobar2</OPTION>
<OPTION value=&quot;foobar3&quot;>foobar3</OPTION>
<OPTION value=&quot;foobar4&quot;>foobar4</OPTION>
<OPTION value=&quot;foobar5&quot;>foobar5</OPTION>
<OPTION value=&quot;foobar6&quot;>foobar6</OPTION>
</SELECT>
</FORM>
</BODY>
</HTML> BDC.
 
OK, thanks. I thought it might be something like this. Strange that the selected items aren't visible in the listbox at the start, though!

The code doesn't do exactly what I need, but it's easy enough to modify. Thanks for the quick response!

Si
 
Modified it. Here it is, in case anyone's interested:

function select_option (selectbox) {
// Clears selected items in listbox, and re-selects them
// This displays items in the visible part of the list!
var selection = new Array (selectbox.options.length);
for (var i=0; i<selectbox.options.length; i++) {
if (selectbox.options.selected) selection = true; else selection = false;
selectbox.options.selected = false;
}
for (var i=0; i<selectbox.options.length; i++) {
if (selection) selectbox.options.selected = true;
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top