Multiple selection in list boxes using VBScript

javascript Pakistan
  • 18 years ago

    Dear Developers


    I hope that some one of you can help me in this problem. the problem is that


    I am using a List box on my web page with multiselection option true.
    I want to check which of the item is selected and which is not.


    the list box contains six items and i want to check which is selected and which is not.


    My dear visitor i'll be very gratefull if you will solve it for me.
    thanx


    Muhammad Jamil

  • 18 years ago

    Im a beginner in HTML/JAVASCRIPT so code might be not the best way 2 do it ...anyway, woks...


    Copy this and save it as HTML...()


    Code:

    <html>
    <head>
    <script LANGUAGE="JavaScript">
    function showSelected()
    {
       //reference 2 list
       var objList = document.getElementById('selName');
       //array containing selected items
       var arrSelectedItems = new Array;
       //array containing not selected items
       var arrNonSelectedItems = new Array;
       //number of selected items
       var intSelectedItems = 0;
       //number of not selected items
       var intNonSelectedItems = 0;
       
       //gettin' selected items
       for(var iCounter = 0 ; iCounter<objList.length ; iCounter++)
           {
               if (objList[iCounter].selected==true)
               {
                   arrSelectedItems[intSelectedItems++] = objList[iCounter].innerText;
               }
               else
               {
                   arrNonSelectedItems[intNonSelectedItems++] = objList[iCounter].innerText;
               }
           }
       //show 'results'
       if (intSelectedItems ==0)
       {
           document.write("<b>No items selected")
       }
       else
       {
           document.write("<b>Selected Items R:</b><BR>");
           for (var iCounter = 0 ; iCounter<intSelectedItems ; iCounter++)    
           {
               document.write('<b>'+(iCounter+1) + ') </b><font color = red>'+ arrSelectedItems[iCounter]+"</font><BR>");
           }
           document.write("<b>NOT Selected Items R:</b><BR>")
           for (iCounter = 0 ; iCounter<intNonSelectedItems ; iCounter++)    
           {
               document.write('<b>'+(iCounter+1) + ') </b><font color = red>'+ arrNonSelectedItems[iCounter]+"</font><BR>");
           }
       }
    }
    </script>
    </head>
    <body>
    <INPUT style="background-color:BBBBBB;" TYPE="BUTTON" SIZE="24" value = "Show selected" onclick = "showSelected()">
    <form name="frmName" id="frmName">
       <select style="background-color:BBBBBB;" name="selName" multiple = true size="6" id="selName">
               <OPTION value="Red">Red</OPTION>
               <OPTION value="Blue">Blue</OPTION>
               <OPTION value="Green">Green</OPTION>
               <OPTION value="Black">Black</OPTION>
               <OPTION value="Yellow">Yellow</OPTION>
               <OPTION value="Orange">Orange</OPTION
       </SELECT>
    <BR><BR>    


    </form>
    </body>
    </html>




    Hope this helps!




    Xcuse my english!!

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Measuring programming progress by lines of code is like measuring aircraft building progress by weight.” - Bill Gates