Skip to content

Instantly share code, notes, and snippets.

@sleepdeprecation
Last active March 20, 2024 15:39
Show Gist options
  • Save sleepdeprecation/1332294 to your computer and use it in GitHub Desktop.
Save sleepdeprecation/1332294 to your computer and use it in GitHub Desktop.
Duplicate a select list using jQuery
<!DOCTYPE html>
<html>
<head>
<title>Duplicate a select list using jQuery</title>
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="selectScript.js"></script>
</head>
<body>
<div id="d1">
<select id="origSel">
<option value="0">Zero</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</div>
<div id="d2"></div>
</body>
</html>
$(document).ready(function () {
// copy the select area
$('#origSel').clone().attr('id', 'newSel').attr('name', 'newSel').appendTo($('#d2'));
// make the current value selected
$("#newSel > option[value='" + $('#origSel').val() + "']").attr('selected', 'selected');
});
@vince844
Copy link

Hi, how would you do if the list comes fro ajax call? many thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment