Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!