Created
February 9, 2009 20:54
-
-
Save roykolak/60983 to your computer and use it in GitHub Desktop.
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 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