how to remove selected item of list box after clicking on “ Delete Selected Item “ and Reset all items of listbox after clicking “Reset” button using JavaScript like .
Figure 1
After clicking on Delete Selected Item
Figure 2
And after clicking on Reset Button
Figure 3
For these add listbox inside body tag and add two button Delete Selected Item and Reset
- <asp:ListBox ID="lstbox" runat="server" SelectionMode="Multiple" Height="150px" Width="100px" >
- <asp:ListItem Value="1" Text ="Class 1"></asp:ListItem>
- <asp:ListItem Value="2" Text ="Class 2"></asp:ListItem>
- <asp:ListItem Value="3" Text ="Class 3"></asp:ListItem>
- <asp:ListItem Value="4" Text ="Class 4"></asp:ListItem>
- <asp:ListItem Value="5" Text ="Class 5"></asp:ListItem>
- <asp:ListItem Value="6" Text ="Class 6"></asp:ListItem>
- <asp:ListItem Value="7" Text ="Class 7"></asp:ListItem>
- </asp:ListBox>
- <button onclick="deleteSelect('lstbox');return false;">Delete Selected Item</button>
- <button onclick="window.location.reload();return false;">Reset</button>
And javaScript in head Tag
- <script>
- function deleteSelect(listid) {
- var listb = document.getElementById(listid);
- var len = listb.options.length;
- for (var i = listb.options.length-1 ; i >= 0 ; i--) {
- if (listb.options[i].selected == true) {
- listb.options.remove(i);
- }
- }
- }
- </script>
And now run the code
It May help to Anyone