populating listbox in ajax

hi
i am trying to populat listbox from the datagrid.datagrid i am using has checkboxes and when the user checks the checkboxes and then clicks the submit button,then the checked items should be displayed in listview control.i am using ajax--jscript.
thnks in advance
pranathi27Asked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
Assumming you have:
<select id="myList" name="DropdownList"></select>
<input type="checkbox" name="dept" value="Engineering"/>
<input type="checkbox" name="dept" value="Mathematics"/>

You can try:
var chkBoxes=document.getElementsByName("dept");
var sel=document.getElementById("myList");
var options="";
for(var i=0; i < chkBoxes.length;++i)
{
  if(chkBoxes[i].checked)
    options+="<option value='"+chkBoxes[i].value+"'>"+chkBoxes[i].value+"</option>";
}
sel.innerHTML=options;
0
 
ilairdConnect With a Mentor Commented:
the above is good, one addition i would suggest would be to stay away from string concats to append nodes. use the following to add to the box, once you get your data back from the ajax return.

function fillSelect(elem, arr)
{
    elem.innerHTML = '';
    for(var i = 0; i < arr.length; i++)
    {
        var nOpt = document.createElement("option");
        var nText = document.createTextNode(arr[i]);
        nOpt.value = arr[i];
        nOpt.appendChild(nText);
        elem.appendChild(nOpt);
    }
}

Open in new window

0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.