I’ve made this function named duplicateDiv() that should duplicate a div when clicking on a button.
It looks like this:
function duplicateDiv(divID, parentDiv) {
let element = document.getElementById(divID);
let clone = element.cloneNode(true);
let container = document.getElementById(parentDiv);
container.appendChild(clone);
}
The button that is being clicked looks like this:
<button
id="addExerciseFriday"
class="addExercise"
onclick="duplicateDiv(programFriday, outerProgramFriday)"
>
The parameters passed into the function on the “onclick” on the button are ID’s of the div I want to copy and the ID of the parent.
Why doesn’t this work?
I’ve had success, but only if I write the function like this:
function duplicateDiv() {
let element = document.getElementById("programFriday");
let clone = element.cloneNode(true);
let container = document.getElementById("outerProgramFriday");
container.appendChild(clone);
and the button looks like this:
<button
id="addExerciseFriday"
class="addExercise"
onclick="duplicateDiv()"
>
But since I need this function to work on 6 other buttons on my page I don’t want to make several functions to each button. Defeats the purpose of a function, really 😛
Thanks in advance 🙂
I’ve tried many variations to the parameters names, doesn’t work.
I’ve tried changing the document.getElementByID(“divID”) & document.getElementByID(“parentDiv”)
But nothing has worked yet…