Adding Elements with JavaScript [duplicate]

0

I'm getting the error:

  

Uncaught DOMException: Failed to execute 'insertBefore' on 'Node': The   node before which the new node is to be inserted is not a child of   this node.

When I try to insert one div after another using JavaScript.

function verificaHierarquia(e) {
    var valor = $(e).val(),
        valor2 = $(e).children(':selected').data("valor2");

    if (valor2 == 1) {
        var divSupervisor = document.createElement("div");
        divSupervisor.id = "divSupervisor";
        divSupervisor.className = "col-lg-6 mb-3";

        var divAtual = document.getElementById("divSenha");
        divAtual.insertBefore(divSupervisor, divSenha);

        document.getElementById("divSupervisor").innerHTML =
            '<label for="sup">Sup</label>\n'+
            '<input type="text" name="sup" class="form-control">';
    }
}
    
asked by anonymous 26.09.2018 / 16:45

1 answer

0

The second parameter of the Node.insertBefore method is a reference node for JS to know which part of the parent element will be inserted.

By the exception received, it seems that divSenha is not the child of divAtual , so JS does not know where to insert the new node.

    
26.09.2018 / 16:59