Add Items to ASP.Net ListBox using JavaScript

Last Reply on Mar 11, 2015 12:00 AM By Mudassar

Posted on Mar 10, 2015 07:42 AM

 Show promt box and add item with text and value to ListBox using JavaScript in ASP.Net

 

 

Posted on Mar 11, 2015 12:00 AM

This way

<asp:ListBox ID="ListBox1" runat="server" Width="150" Height="60" SelectionMode="Multiple">
</asp:ListBox>
<br />
<br />
<asp:Button ID="btnAdd" Text="Add" runat="server" OnClientClick="return AddValues()" />
<script type="text/javascript">
    function AddValues() {
        var value = prompt("Please enter values.", "");
        var listBox = document.getElementById("ListBox1");
        var option = document.createElement("OPTION");
        option.innerHTML = value;
        option.value = value;
        listBox.appendChild(option);
        return false;
    }
</script>