I am currently working on dynamically creating an input field using the append child method along with add and delete buttons to form a tree-like structure. The goal is to be able to create child nodes with add and delete buttons upon clicking the add button. Additionally, clicking the add button within a child node should generate a sub-child node. While I have achieved this manually, I am now looking for a way to dynamically add child and sub-child nodes for any number of iterations using the append child method in JavaScript.
Furthermore, upon clicking the delete button, the parent node should be removed. How can I retrieve the dynamically generated ID of the parent node?
function add_div(){
var div1 = document.createElement('ul');
document.body.appendChild(div1);
div1.className = 'ui-modal';
div1.id = 'test-' + document.querySelectorAll('.ui-modal > .msg1').length;
var check = div1.id;
var list = document.createElement('li');
list.className = 'msg1';
div1.appendChild(list);
var input = document.createElement('input');
list.appendChild(input);
input.placeholder="parent";
var button1 = document.createElement('button');
list.appendChild(button1);
var t1 = document.createTextNode("ADD");
button1.appendChild(t1);
button1.addEventListener("click", add_div2);
var button2 = document.createElement('button');
list.appendChild(button2);
var t2 = document.createTextNode("DEL");
button2.appendChild(t2);
button2.addEventListener("click", remove_div);
function add_div2(){
var div2 = document.createElement('ul');
div1.appendChild(div2);
var lists = document.createElement('li');
div2.appendChild(lists);
div2.className = 'sub-div';
div2.id = 'sub_test-' + document.querySelectorAll('.sub-div > .msg2').length;
lists.className = 'msg2';
div2.appendChild(lists);
var inputs = document.createElement('input');
lists.appendChild(inputs);
inputs.placeholder="child";
var button1s = document.createElement('button');
lists.appendChild(button1s);
var t1s = document.createTextNode("ADD");
button1s.appendChild(t1s);
button1s.addEventListener("click", add_div3);
var button2s = document.createElement('button');
lists.appendChild(button2s);
var t2s = document.createTextNode("DEL");
button2s.appendChild(t2s);
button2s.addEventListener("click", remove_div);
}
function add_div3(){
var div3 = document.createElement('ul');
div1.appendChild(div3);
var listss = document.createElement('li');
div3.appendChild(listss);
div3.className = 'inner-sub-div';
div3.id = 'sub_test-' + document.querySelectorAll('.inner-sub-div > .msg3').length;
listss.className = 'msg3';
div3.appendChild(listss);
var inputss = document.createElement('input');
listss.appendChild(inputss);
inputss.placeholder="sub child";
var button1ss = document.createElement('button');
listss.appendChild(button1ss);
var t1ss = document.createTextNode("ADD");
button1ss.appendChild(t1ss);
var button2ss = document.createElement('button');
listss.appendChild(button2ss);
var t2ss = document.createTextNode("DEL");
button2ss.appendChild(t2ss);
button2ss.addEventListener("click", remove_div);
}
function remove_div(){
var check = document.getElementById('test-0');
check.parentNode.removeChild(check);
}
}
.ui-modal{
width: 200px;
border: 1px solid red;
position: relative;
left:0;
z-index: 55;
}
.sub-div{
margin-top: 10px;
width: 200px;
height: 50px;
left: 100px;
border: 1px solid blue;
position: relative;
z-index: 66;
}
.inner-sub-div{
margin-top: 10px;
width: 200px;
left: 250px;
border: 1px solid blue;
position: relative;
z-index: 77;
}
.plus{
border: 1px solid red;
display: inline-block;
}
<div class="wrapper">
<input type="button" value="ADD" onclick="add_div();">
</div>
The buttons are dynamically generated using append child method with an added event listener for the add button, which appends an input field with add and delete buttons upon clicking. While clicking the add button successfully adds nodes, I am facing difficulties in retrieving the ID on clicking the delete button.
I have manually deleted a parent node with the ID test-0.
function remove_div(){
var check = document.getElementById('test-0');
check.parentNode.removeChild(check);
}
However, I am looking for a dynamic solution to delete nodes. How can I retrieve the specific ID of the parent node for deletion?
All of these actions need to be performed dynamically as I have manually assigned names to each field.