rigth now I have this code to draw some divs (based on some logic I’ll have parent and childs.
JavaScript
x
22
22
1
array.forEach(async (a, b) => {
2
var currentDiv = 0;
3
let divHtml;
4
5
if (b === 0) {
6
//This should be the first parent
7
divHtml = document.createElement('div');
8
divHtml.id = 'someID' + currentDiv;
9
} else if (previousDiv != currentDiv) {
10
// This should be used for next parents based on the previousDiv & currentDiv logic
11
divHtml = document.createElement('div');
12
divHtml.id = 'someID' + currentDiv;
13
} else {
14
// This should be used only for childs. I want to create another div but inside the parent that I have stored into currentDiv.
15
divHtml = document.getElementById('someID' + currentDiv);
16
divHtml.id = 'someChildID' + currentDiv;
17
}
18
19
// Some more code below but what it's important is this (I continue using the divHtml in a lot of places on my code):
20
divHtml.setAttribute('someData', someDataAttribute);
21
});
22
So, my question is: if there is a way to get the parentDiv and draw inside the X childs elements and how can I do it? I tried with this:
JavaScript
1
2
1
divHtml = document.createElement('div').appendTo($('#someID' + currentDiv));
2
But I’m getting the .appendTo() is not a function error message.
Advertisement
Answer
.appendTo()
is a jQuery
method, it seems you are using plain javascript, maybe you want to use append
or appendChild