jQuery Clone (Copy) Dropdown List with Selected Value

jQuery Clone (Copy) Dropdown List with Selected Value

Jul 9, 2014
Introduction

Here I will explain how to use 
jQuery to copy or clone dropdown list with selected option or jQuery copy dropdown value to another dropdown.

Description
  
In previous articles I explained jQuery get radio button selected value by namejQuery pdf viewer plugin examplesjQuery news ticker plugin examples
jQuery dropdown with imagesjQuery Countdown timer script example and many articles relating to jQuery in asp.net. Now I will explain how to get clone dropdown list using jQuery.

If we want to clone dropdown list using jQuery we need to write the code like as shown below


$('#ddlcountry').clone().attr('id''choices_' + $(this).index()).insertAfter("#ddlcountry");


If you want to check it in complete sample we need to write the code like as shown below


<html>
<head id="Head1">
<title>jQuery Clone Dropdown list with selected value</title>
</head>
<body>
<select id="ddlcountry">
<option value="0">select</option>
<option value="india">india</option>
<option value="usa">usa</option>
<option value="canada">canada</option>
</select>
<input type="button" id="btnclone" value="Clone Dropdown">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function() {
$("#btnclone").click(function() {
$('#ddlcountry').clone().attr('id''choices_' + $(this).index()).insertAfter("#ddlcountry");
});
});
</script>
</body>
</html>

Live Demo

To check live demo try to click on below button to create new dropdown list with selected value


Clone Dropdown:   

Comments