Last active
April 4, 2020 09:00
-
-
Save calvinchoy/5796702 to your computer and use it in GitHub Desktop.
JS - Child duplicator #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
$(".btn-add-child").click(function() { | |
var newItem; | |
newItem = $("body .child.item.duplicate").clone().removeClass("duplicate"); | |
newItem.appendTo(".parent"); | |
$(".parent .child.item").show(); | |
return $(".btn-remove-child").click(function() { | |
return $(this).parent().remove(); | |
}); | |
}); | |
$(".btn-remove-child").click(function() { | |
return $(this).parent().remove(); | |
}); | |
//Example html | |
/* | |
<div class="parent"> | |
<div class="child item"> | |
<!-- start: child content --> | |
<select name="selectbox" id="selectbox"> | |
<option value="val 1">val 1</option> | |
<option value="val 2">val 2</option> | |
<option value="val 3">val 3</option> | |
<option value="val 4">val 4</option> | |
</select> | |
<!-- end: child content --> | |
<span class="btn-remove-child">X</span> | |
</div> | |
</div> | |
<!-- DUPLICATE child item added further down before </body> --> | |
<button class="btn-add-child">Add child</button> | |
*/ | |
//Child item to repeat | |
/* | |
<!-- start: child item duplicate --> | |
<div class="child item duplicate"> | |
<select name="selectbox" id="selectbox"> | |
<option value="val 1">val 1</option> | |
<option value="val 2">val 2</option> | |
<option value="val 3">val 3</option> | |
<option value="val 4">val 4</option> | |
</select> | |
<span class="btn-remove-child">X</span> | |
</div> | |
<!-- end: child iem duplicate --> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment