How we can select all list box and Deselect All list box using javascript



And when user click Deselect All button then all item of list box deselected like



For these add listbox inside body tag and add two button Select All and Deselect All
  1. asp:ListBox ID="lstbox" runat="server" SelectionMode="Multiple" Height="150px" Width="100px" >  
  2.             <asp:ListItem Value="1" Text ="Class 1"></asp:ListItem>  
  3.             <asp:ListItem Value="2" Text ="Class 2"></asp:ListItem>  
  4.             <asp:ListItem Value="3" Text ="Class 3"></asp:ListItem>  
  5.             <asp:ListItem Value="4" Text ="Class 4"></asp:ListItem>  
  6.             <asp:ListItem Value="5" Text ="Class 5"></asp:ListItem>  
  7.             <asp:ListItem Value="6" Text ="Class 6"></asp:ListItem>  
  8.             <asp:ListItem Value="7" Text ="Class 7"></asp:ListItem>  
  9.         </asp:ListBox>  
  10.          <button onclick="selectDeselect('lstbox', true);return false;">Select All</button>  
  11.         <button onclick="selectDeselect('lstbox', false);return false;">Deselect All</button>  
And javaScript in head Tag
  1. script>  
  2.         function selectDeselect(listid, status) {  
  3.             var listb = document.getElementById(listid);  
  4.             var len = listb.options.length;  
  5.             for (var i = 0; i < len; i++) {  
  6.                 listb.options[i].selected = status;  
  7.             }  
  8.         }  
  9.         
  10.       </script>  
And now run the code

It May help Anyone!!
Ebook Download
View all
Learn
View all