Skip to content Skip to sidebar Skip to footer

Get A Listbox's Selected Items In Javascript

I have two listboxes in asp.net. On the click of a button I want to load a list box with the elements of the selected items in the other box. The problem is that this has to be don

Solution 1:

Try:

//onclick for button calls this functionfunctionUpdatelist() {
 var sel = document.getElementbyId("list1");
 var listLength = sel.options.length;
 for(var i=0;i<listLength;i++){
    if(sel.options[i].selected)
    document.getElementById("list2").add(newOption(sel.options[i].value));
  }                  

Solution 2:

more precisely we can do it like;

functionselectedVal(list) {   
    alert(list.options[list.selectedIndex].text);                  
} 

<select id="listbox" multiple="multiple" 
        style="height: 300px; width: 200px;" 
        onclick="javascript:selectedVal(this);">  
</select>

Solution 3:

Here is a good article on how to do this using Jquery.

You could also stick your drop downs in an Update Panel.

Solution 4:

functionUpdatelist() {
var sel = document.getElementById('<%=ListBox1.ClientID%>')
var lst2 = document.getElementById('<%=ListBox2.ClientId %>')
var listLength = sel.options.length;
var list2length = lst2.options.length;
for (var i = 0; i < listLength; i++) {
    if (sel.options[i].selected) {
        //lst2.options.add(sel.options[i].text);
        lst2.options[list2length] = newOption(sel.options[i].text);
        list2length++;
    }
}

}

Post a Comment for "Get A Listbox's Selected Items In Javascript"