Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a list box like this
ASP.NET
<select id="ddlBranch" name="ddlBranch" multiple runat="server" style="width:100%;">
</select>

in this listbox there are no of items

on blur of textbox i am fetching some values from database
I need whatever items i am retrieving matching with listbox should be all selected in list box

with the help of following script i am getting able to selected only one value from listbox
C#
function getSelet()
           {
                 if (document.getElementById('hidBranchLst').value != '') {
                var strBranch = document.getElementById('hidBranchLst').value
                var strBranchList = strBranch.split('|');
                var MulSel = document.getElementById('ddlBranch');
                MulSel.multiple = true;
                for (var j = 0; j < strBranchList.length; j++) {
                    alert(strBranchList[j].toString());
                    for (var i = MulSel.options.length - 1; i >= 0; i--) {
                        if (MulSel.options[i].value == strBranchList[j].toString()) {
                            MulSel.selectedIndex = i;
                        }
                    }
                }
            }
        }
Posted
Updated 22-Aug-11 23:21pm
v2

1 solution

Please have a look at this thread:

http://www.webdeveloper.com/forum/showthread.php?t=34494[^]

Currently you are setting the selectedIndex of the list box but that will only select one. You need to set each option as being selected, as shown in the above thread. Instead of MulSel.selectedIndex = i; you should try MulSel.options[i].selected = true;.

Hope this helps,

Ed :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900