Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > HTML > HTML Code Clinic
|
HTML Code Clinic Do you have some HTML code you'd like to share and get suggestions from others for tweaking or improving it? This discussion is the place.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the HTML Code Clinic section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 4th, 2004, 03:42 PM
Authorized User
 
Join Date: Sep 2004
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ctrl Click with SELECT MULTIPLE

Hi, All,

I have a list box, which when user makes selection, I will check the selected value and display something on screen.
My question: How do I detect which value has been selected when user use Ctrl-Click to select multiples?

Thanks.

 
Old October 5th, 2004, 03:14 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi,

One way is to use the onchange event to handle a change in selection, then check the selected property of each of each option in the select box.

Code:
<select name="mySelect" id="mySelect" multiple="multiple" onchange="ShowSelected(this);">
...some options...
</select>

...

function ShowSelected(pBox){
    var temp = "";
    for(var i = 0; i < pBox.options.length; i++){
        if(pBox.options[i].selected){
            temp += pBox.options[i].text + "\n";
        }
    } 
    alert("You selected: \n" + temp);
}
HTH,

Chris

 
Old October 5th, 2004, 07:01 AM
Authorized User
 
Join Date: Sep 2004
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, but what about I want to catch the very one that user just selected? Instead of going thru the entire loop, especially when user uses [u]Ctrl-Click </u>to select multiple? I mean, I know the first they click, but what about the subsequent onces with Ctrl-Click?
 
Old October 5th, 2004, 07:51 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hey,

The following works for me, using an array to store the state after each selection (AFAIK there is no single property you can use)...

Code:
var gSelected = [];
function ShowSelected(pBox){
    var temp = "";
    for(var i = 0; i < pBox.options.length; i++){
        if(pBox.options[i].selected != gSelected[i]){
            gSelected[i] = pBox.options[i].selected;

            if(gSelected[i]){
                alert("you selected " + pBox.options[i].text);
            }
        }
    } 
}
Cheers,

Chris

 
Old October 5th, 2004, 12:14 PM
Authorized User
 
Join Date: Sep 2004
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, this helps!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Select the all itmes in listbox by pressing Ctrl+ gmbalaa General .NET 1 August 22nd, 2007 02:17 AM
CodeBehind: Multiple ImageButtons, One Click Event bsausser ASP.NET 1.0 and 1.1 Basics 11 October 18th, 2006 10:50 PM
Save (output to) multiple files from single click moo_desanta Classic ASP Basics 2 June 23rd, 2006 02:00 AM
Multiple windows on click of link spacy Javascript How-To 2 April 11th, 2005 05:57 AM
multiple checkboxes with same name click events kcs_chandra Classic ASP Databases 3 October 24th, 2004 08:09 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.