Unselecting specific item in listbox

D

Doogie

Hi,
I have a listbox set up so users can select multiple choices. I want
to limit them so they can choose no more than 4 at a time. I have set
up javascript code to do the check on the number of choices selected
but what I want to do is if they choose more than 4 (i.e. 5), I want
to unselect the last item they selected. I cannot figure out how to
determine which was the last item selected.

function HasExceededMaximumYearWeekCount()
{
var count = 0;
for (index = 0; index < fmDetailQuery.lstYearWeek.options.length;
index++)
{
if (document.all.lstYearWeek.options(index).selected)
{
count++;
}
}

if (count > 4)
{
alert("You can only select a maximum of 4 year weeks at a
time.")
return false;
}

return true;

}

I have tried doing a check on this value:

fmDetailQuery.lstYearWeek.selectedIndex

but it always returns me the first item in the listbox that was
selected (i.e. the one at the top of the list that was selected). I
want the last item the user selected, is there a way to determine that?
 
G

GArlington

Hi,
I have a listbox set up so users can select multiple choices. I want
to limit them so they can choose no more than 4 at a time. I have set
up javascript code to do the check on the number of choices selected
but what I want to do is if they choose more than 4 (i.e. 5), I want
to unselect the last item they selected. I cannot figure out how to
determine which was the last item selected.
<snap>
You will need onClick event on each checkbox to do that...
 
D

Doogie

<snap>
You will need onClick event on each checkbox to do that...

It's not a checkbox, it's a listbox. I need a way to determine the
last item selected in the list box.
 
G

GArlington

It's not a checkbox, it's a listbox. I need a way to determine the
last item selected in the list box.

I have done something like that a long time ago, I think that the
trick was in (still) onClick event, it returned info about which item
was clicked (and therefore selected/deselected)...
 
P

pr

Doogie said:
It's not a checkbox, it's a listbox. I need a way to determine the
last item selected in the list box.

onclick in the select element then. To determine which item(s) is/are
selected, look at the selected property of each option. Simple example:

var i, l, a = [];
for (i = 0, l = selectElement.options.length; i < l; i++) {
if (selectElement.options.selected) {
a.push(i);
}
}
window.alert("Selected items were " + a.join(", "));

Set selected to false to unhighlight an option. You might like to
consider check boxes, however; multiple selects can be a pain for the
end user - eg. try using them with just the keyboard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top