Last active
November 29, 2024 14:24
-
-
Save sleepdeprecation/1332294 to your computer and use it in GitHub Desktop.
Duplicate a select list using jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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
Hi, how would you do if the list comes fro ajax call? many thanks