473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Unselecting specific item in listbox

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
5 2294
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Peter Moscatt | last post by:
I am having trouble understanding the methods for the Listbox from Tk. If I was to select at item in the list using a mouse click (have already created the bind event) - what method returns the...
3
by: Paul T. Rong | last post by:
I have a listbox (of product names) control on my form. I want to pass the selected item (a product name) to a subform, and the product unitprice should apear automatically next to the product name...
10
by: SueB | last post by:
Hi. Is there a way to "unselect" items in a listbox, programmatically? Here's what I want to do (and I've been trying to to this while debugging the form, but I've been unsuccessful) ... I...
1
by: honeybee | last post by:
How to get the item value from a specific Item Index in the listbox?
4
by: rn5a | last post by:
I am binding a DropDownList with records existing in a database table. I want to add an extra item *SELECT COMPANY* at index 0 so that by default, it gets selected. This is how I tried it but the...
6
by: jesse07 | last post by:
I have 6 seperate listbox and I want the user to select 1 value from 1 of the 6 boxes. The problem is after the user clicks the listbox1 then listbox2, listbox1 still says highlighted. I want to...
8
by: jh | last post by:
I'd like to copy/paste into a listbox during runtime. I can do this for a textbox but can't figure out how to accomplish this for a listbox. Any help? Thanks.
2
by: gnosys | last post by:
In ASP.Net 1.1 using C#, I'm trying to dynamically change the background colors of certain listbox items based on some criteria. For example:
3
by: Sweetiecakes | last post by:
Hi I have an string array containing file paths. I have added them to a ListBox with AddRange(); When I delete an item from the ListBox, how can I delete the same item from the original...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.