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

183
Visualizações
transferir contenido de un elemento a otro elemento con javascript

¿Cómo transferir la lista A a la lista B haciendo clic en BtnB? Con mi código logro transferir el contenido haciendo clic en el BTNB, pero cuando vuelvo a hacer clic en el BTNA, los números ya no aparecen en la lista A como se esperaba sino en la lista 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 Respostas
Responde à pergunta

0

Estás transfiriendo todo el elemento listB listA no solo su contenido.

Debe recorrer los elementos secundarios de listA y transferir cada uno de ellos.

Convierto source.childNodes en una matriz para la iteración porque mover los nodos los elimina de la lista de childNodes , lo que haría que la iteración se salte los nodos.

 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 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