Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mjurincic/4de918e60df64fdc796a70ac716878a5 to your computer and use it in GitHub Desktop.
Save mjurincic/4de918e60df64fdc796a70ac716878a5 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');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment