Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

195
Visualizações
Error trying to overlay and remove elements with removeChild

i have a problem in the following code snippet.

for (var i=0;i<data.parlist.length;i++){
            var inp = document.createElement("input")
            var label = document.createElement("label")
            var labelValue = document.createElement("label")
            var subDiv = document.createElement("div")
            var br = document.createElement("br")
            try{
                var dBtn = document.getElementById(data.parlist[i].id);
                var dLabel = document.getElementById(data.parlist[i].id + " label")
                
                var dDiv = document.getElementById(data.parlist[i].id +" div");

                var inprad = document.getElementById(data.parlist[i].id+" inp_rad");
                var dLabelRad = document.getElementById(data.parlist[i].id + " label_rad")

                var dSubDiv = document.getElementById(data.parlist[i].id +" div_rad");

                dSubDiv.replaceChild(inprad);
                dSubDiv.removeChild(dLabelRad);

                dDiv.removeChild(dSubDiv);
                
                dDiv.removeChild(dBtn);
                dDiv.removeChild(dLabel);
                
                myDiv.removeChild(dDiv);
                
            }catch{
                console.log("no")
            }

This for loop is inside a function that when clicking on a button of a station shows the parameters.in this way:

enter image description here

Shows station parameters

What you see in the images works, but the problem that I have come to try to clarify is that when I press the button for example as station 1 appears in the image and then I press the button for example of station 2 the try that is supposed to you must remove the previous parameters with removeChild does not work, the parameters of station 1 remain in the div and the parameters of station 2 are added below these instead of being removed

Shows how the parameters are added below the other parameters

Within the try there is a problem but I do not know what it is, I say this because when I check the page and open the console I get the message that I left in the catch a "no"

The "no" on the console

Thanks for reading my problem and any suggestions are welcome as I am desperate to find a solution.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to pass 2 inputs to the replaceChild function.

parentNode.replaceChild(newChild, oldChild);

For more info refer Mozilla docs here.

Here is some additional advice

  1. When you log for debugging, use some meaningful messages. In your case you could do
try {
 ...
} catch (err) {
   console.log(err);
}
  1. From the questions nothing is clear about what is there in data.parlist and whether that is implemented properly.
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda