Skip to content

Instantly share code, notes, and snippets.

@geilt
Last active December 12, 2015 07:28
Show Gist options
  • Save geilt/4736938 to your computer and use it in GitHub Desktop.
Save geilt/4736938 to your computer and use it in GitHub Desktop.
Allows you to clone an element with select data.
/**
* 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