In this article I will explain with an example, how to dynamically add (insert) and remove (delete) DropDownLists on Button click using jQuery.
When the Add Button is clicked, DropDownLists will be dynamically created and added to the Page and when the Remove Button is clicked, the DropDownLists will be removed from the Page.
 
 
Dynamically Add (Insert) and Remove (Delete) DropDownLists on Button click using jQuery
The following HTML Markup consists of an HTML Button which when clicked will dynamically create and add DropDownLists and an HTML DIV to which the dynamic DropDownLists will be added.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <input type="button" id = "btnAdd" onclick = "AddDropDownList()" value = "Add DropDownList" />
    <hr />
    <div id = "dvContainer"></div>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        function AddDropDownList() {
            //Build an array containing Customer records.
            var customers = [
                { CustomerId: 1, Name: "John Hammond", Country: "United States" },
                { CustomerId: 2, Name: "Mudassar Khan", Country: "India" },
                { CustomerId: 3, Name: "Suzanne Mathews", Country: "France" },
                { CustomerId: 4, Name: "Robert Schidner", Country: "Russia" }
            ];
            //Create a DropDownList element.
            var ddlCustomers = $("<select />");
 
            //Add the Options to the DropDownList.
            $(customers).each(function () {
               var option = $("<option />");
 
                //Set Customer Name in Text part.
                option.html(this.Name);
 
                //Set CustomerId in Value part.
                option.val(this.CustomerId);
 
                //Add the Option element to DropDownList.
                ddlCustomers.append(option);
            });
 
            //Reference the container DIV.
            var dvContainer = $("#dvContainer")
 
            //Add the DropDownList to DIV.
            var div = $("<div />");
            div.append(ddlCustomers);
 
            //Create a Remove Button.
            var btnRemove = $("<input type = 'button' value = 'Remove' />");
            btnRemove.click(function () {
                $(this).parent().remove();
            });
 
            //Add the Remove Buttton to DIV.
            div.append(btnRemove);
 
            //Add the DIV to the container DIV.
            dvContainer.append(div);
        };
    </script>
</body>
</html>
 
Explanation:
Adding Dynamic DropDownList
When the Add button is clicked, the AddDropDownList JavaScript function is called. Inside the JavaScript function, first a JSON Array is generated.
Then a dynamic DropDownList element is created and then using loop all items from the JSON array are added to the dynamic DropDownList element.
Note: For more details on populating HTML DropDownList using JSON Array, please refer my article Populate DropDownList from JSON Array using jQuery.
 
Once populated the dynamic DropDownList is added to the HTML DIV which later will be added to the main Container HTML DIV.
 
Deleting Dynamic DropDownList
For the Delete functionality, a dynamic Button named Remove is created and added to the HTML DIV which contains the DropDownList.
The Remove button is also assigned with an OnClick event handler inside which, first the reference the child HTML DIV which contains the DropDownList and also the Remove Button is determined and then using the reference, the HTML DIV is removed.
 
 
Screenshot
Dynamically Add (Insert) and Remove (Delete) DropDownLists on Button click using jQuery
 
 
Demo
 
 
Browser Compatibility

The above code has been tested in the following browsers.

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.

 
 
Downloads