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

175
Views
transfer content from one element to another element with javascript

How to transfer list A to list B by clicking on BtnB? With my code, I manage to transfer the content by clicking the BTNB, but when I click again on the BTNA, the numbers no longer appear in list A as expected but in list B

document.getElementById("buttonTryA").onclick = takeNumberA;

function takeNumberA() {
  var x = document.getElementById("myNumber").value;
  document.getElementById("listA").appendChild(document.createTextNode(`${x} `));
}

document.getElementById("buttonTryB").onclick = appendIt;

function appendIt() {
  var source = document.getElementById("listA");
  document.getElementById("listB").appendChild(source);
}
section {
  display: flex;
  flex-wrap: wrap;
}

.titleA {
  margin-left: 50px;
  margin-right: 180px;
}

#listNumber {
  width: 200px;
  height: 300px;
  border: thin solid #ccc;
}
<body>
  Number: <input type="number" id="myNumber" min="1" max="9">

  <p>Click the BtnA to display the number of the number field (list A).</p>
  <section>
    <h1 class="titleA"> List A </h1>
    <h1> List B </h1>
  </section>
  <section>
    <button id="buttonTryA">BTNA</button>
    <div id="listNumber">
      <p id="listA"></p>
    </div>
    <button id="buttonTryB">BTNB</button>
    <div id="listNumber">
      <p id="listB"></p>
    </div>
  </section>
</body>

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You're transfering the entire listA element to listB, not just its contents.

You should loop over the children of listA and transfer each of them.

I convert source.childNodes to an array for the iteration because moving the nodes removes them from the childNodes list, which would make the iteration skip nodes.

document.getElementById("buttonTryA").onclick = takeNumberA;

function takeNumberA() {
  var x = document.getElementById("myNumber").value;
  document.getElementById("listA").appendChild(document.createTextNode(`${x} `));
}

document.getElementById("buttonTryB").onclick = appendIt;

function appendIt() {
  var source = document.getElementById("listA");
  Array.from(source.childNodes).forEach(child =>
    document.getElementById("listB").appendChild(child));
}
section {
  display: flex;
  flex-wrap: wrap;
}

.titleA {
  margin-left: 50px;
  margin-right: 180px;
}

#listNumber {
  width: 200px;
  height: 300px;
  border: thin solid #ccc;
}
<body>
  Number: <input type="number" id="myNumber" min="1" max="9">

  <p>Click the BtnA to display the number of the number field (list A).</p>
  <section>
    <h1 class="titleA"> List A </h1>
    <h1> List B </h1>
  </section>
  <section>
    <button id="buttonTryA">BTNA</button>
    <div id="listNumber">
      <p id="listA"></p>
    </div>
    <button id="buttonTryB">BTNB</button>
    <div id="listNumber">
      <p id="listB"></p>
    </div>
  </section>
</body>

over 4 years ago · Santiago Trujillo 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!