By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

Unselecting specific item in listbox

P: n/a
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?
Aug 8 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Aug 8, 2:12 pm, Doogie <dnlwh...@dtgnet.comwrote:
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...

Aug 8 '08 #2

P: n/a
On Aug 8, 9:15*am, GArlington <garling...@tiscali.co.ukwrote:
On Aug 8, 2:12 pm, Doogie <dnlwh...@dtgnet.comwrote: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...
It's not a checkbox, it's a listbox. I need a way to determine the
last item selected in the list box.
Aug 8 '08 #3

P: n/a
On Aug 8, 3:23 pm, Doogie <dnlwh...@dtgnet.comwrote:
On Aug 8, 9:15 am, GArlington <garling...@tiscali.co.ukwrote:
On Aug 8, 2:12 pm, Doogie <dnlwh...@dtgnet.comwrote: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...

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)...
Aug 8 '08 #4

P: n/a
pr
Doogie wrote:
On Aug 8, 9:15 am, GArlington <garling...@tiscali.co.ukwrote:
>On Aug 8, 2:12 pm, Doogie <dnlwh...@dtgnet.comwrote: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...

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[i].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.
Aug 8 '08 #5

P: n/a
pr
pr wrote:
onclick
Sorry, should be onchange
>
[code]
ie. by comparing an array denoting previously selected options with the
list of currently selected options, you know which option(s) the user
last selected.
Aug 8 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.