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

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
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++) {
                    for (var i = MulSel.options.length - 1; i >= 0; i--) {
                        if (MulSel.options[i].value == strBranchList[j].toString()) {
                            MulSel.selectedIndex = i;
Updated 22-Aug-11 23:21pm

1 solution

Please have a look at this thread:[^]

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