Skip to content

Instantly share code, notes, and snippets.

@roykolak
Created February 9, 2009 20:54
Show Gist options
  • Save roykolak/60983 to your computer and use it in GitHub Desktop.
Save roykolak/60983 to your computer and use it in GitHub Desktop.
/* Clone and Insert Attacher -------------------------------- */
AttachOnceClass.add('CloneAndInsert', 'js_clone_and_insert', function(jquery_selector) {
// TODO: conside refactoring into a class
jquery_selector.click(function() {
var parent = $(this).parent();
var insert = parent.find('.js_insert');
var clone = parent.find('.js_clone').clone(true);
var count = insert.children().length + 2;
// Recursively make attributes unique in cloned html
function uniquifyAttributes(children) {
jQuery.each(children, function(index, child) {
child = $(child);
if(child.children()) {
uniquifyAttributes(child.children());
}
jQuery.each(['name','id','for'], function(index, attribute) {
if(child.attr(attribute)) {
var unique = child.attr(attribute) + count;
child.attr(attribute, unique);
}
});
});
}
uniquifyAttributes(clone.children());
clone.removeClass('js_clone');
clone.addClass('js_cloned');
clone.css('display','block')
clone.appendTo(insert);
new Attach.RemoveClone();
return false;
});
});
/* Remove Clone Attacher -------------------------------- */
AttachOnceClass.add('RemoveClone', 'js_remove_clone', function(jquery_selector) {
jquery_selector.click(function() {
var clone = $(this).parent();
clone.parent('.js_cloned').remove();
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment