Home > Back-end >  Create element inside another based on id
Create element inside another based on id

Time:08-08

rigth now I have this code to draw some divs (based on some logic I'll have parent and childs.

array.forEach(async (a, b) => { 
    var currentDiv = 0;
    let divHtml;

    if (b === 0) {
        //This should be the first parent 
        divHtml = document.createElement('div');
        divHtml.id = 'someID'   currentDiv;
    } else if (previousDiv != currentDiv) {
        // This should be used for next parents based on the previousDiv & currentDiv logic
        divHtml = document.createElement('div');
        divHtml.id = 'someID'   currentDiv;
    } else {
        // This should be used only for childs. I want to create another div but inside the parent that I have stored into currentDiv.
        divHtml = document.getElementById('someID'   currentDiv);
        divHtml.id = 'someChildID'   currentDiv;
    }

    // Some more code below but what it's important is this (I continue using the divHtml in a lot of places on my code):
    divHtml.setAttribute('someData', someDataAttribute);
}); 

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:

divHtml = document.createElement('div').appendTo($('#someID'   currentDiv));

But I'm getting the .appendTo() is not a function error message.

CodePudding user response:

.appendTo() is a jQuery method, it seems you are using plain javascript, maybe you want to use append or appendChild

  • Related