Last active
December 12, 2015 07:28
-
-
Save geilt/4736938 to your computer and use it in GitHub Desktop.
Allows you to clone an element with select data.
This file contains 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
/** | |
* Clone Elements by Alexander Conroy | |
* Copyright 2012 Esotech Inc. | |
* MIT License | |
* http://opensource.org/licenses/MIT | |
*/ | |
$(document).on('click', '.clone', function() { | |
// This should be a generic class you give to the element you want to clone. The clone button should be inside the element. | |
var clone = $(this).closest('.skeleton') | |
var newClone = clone.clone(); | |
//Have to clone the selected options. No fun. | |
var values = {}; | |
//Go through all the old elements current values and save them to the value object | |
clone.find('select').each( function() { | |
values[$(this).attr('name')] = $(this).val(); | |
}); | |
//Go through all the new elements current values and reset them from the value object. | |
newClone.find('select').each( function() { | |
$(this).val(values[$(this).attr('name')]); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment