The idea is that I would like to add a text box in each div, that I have created dynamically.
HTML
<!DOCTYPE html>
<html>
<head>
<title>trial </title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--bootstrap setup-->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script src="trial.js"></script>
</head>
<div>
<p> Title</p>
</div>
<div>
<button class = "btn_add">Add </button>
</div>
<!-- level container-->
<div id = "container">
</div>
<!--stuff container -->
<div id="random_container">
</div>
</body>
</html>
JS
$(document).ready(function () {
var i = 1;
$(document).on('click', '.btn_add', () => {
let newElement = document.createElement('div');
newElement.setAttribute("id", "level_" + i);
newElement.setAttribute("class", "panel");
$('#container').append(`<div id="title">\
Level ` + i +`
</div> \
<div>\
<button id =btnadd_`+ i +` class="btn_addStuff"> Add Stuff </button>
</div>`);
$('#container').append(newElement);
i = i + 1;
});
$(document).on('click', '.btn_addStuff', () => {
/**let element = document.getElementById(div = "level_"+i);
element.append(`<div id="card"> \
<textarea placeholder="Title" type="text" maxlength="10" rows="1">Stuff</textarea> \
</div>`);
$('random_container').append(element); */
$('#random_container').append(`<div id="card"> \
<textarea placeholder="Title" type="text" maxlength="10" rows="1">Stuff</textarea> \
</div>`
);
});
});
My idea is that if I click the button add, I should have a div where there is level 1 and the button add stuff. Which it works.
For example, if I click 3 times, the button add, I should have three div, which each contains a button "add stuff" and the name level. What I would like to do, is if I click the button add stuff, it should appear a text box. The problem that I am having with my code, is that I am not able to have the text box in the right position. If I am in the level one and I click 2 times the button add stuff, I should get two text boxes in the level one, and if I decide to click 3 times on the level 2 to get 3 boxes, I should get 3 text boxes.
Instead, what happens, it's that I am having the text boxes only in one level.
What I thought is to try to get the id in the addstuff button and then try to append the code and then add everything in the random_container, but I can't get an element that has created dynamically?
Hope the question is clear.