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

215
Visualizações
Multiple elements using appendChild for table

I have the following input and button

const inputNotice = document.createElement("input");
inputNotice.type = "text";
r.insertCell(26).appendChild(inputNotice.cloneNode(true));

//new button

const Noticebutton = document.createElement("button");
Noticebutton.type = "button";
Noticebutton.textContent = "Send Notice";
r.insertCell(26).appendChild(Noticebutton);

Im trying to get the input field and button to sit in 1 cell in the table, it creates these in 2 separate cells.

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

0

create a div having input and button as children and then append this as the child of the cell like this:

const div= document.createElement('div');

const inputNotice = document.createElement('input');

const Noticebutton = document.createElement("button");

div.appendChild(inputNotice);

div.appendChild(NoticeButton);

about 4 years ago · Juan Pablo Isaza Relatório

0

Since there is no html template provided, I have had one of my own to do the illustration.

The idea is to add both of the elements inside a container and then append that container to the cell as a child.

In the following illustration I have used a div as a container. Feel free to choose one of which that suits the needs.


Illustration

const nonWorkingRow = document.querySelector('#cell-host-non-working');

const inputNotice = document.createElement("input");
inputNotice.type = "text";
// inputNotice.style.display = 'inline block';
nonWorkingRow.insertCell(0).appendChild(inputNotice.cloneNode(true));

//new button

const Noticebutton = document.createElement("button");
Noticebutton.type = "button";
Noticebutton.textContent = "Send Notice";
// Noticebutton.style.display = 'inline block';
nonWorkingRow.insertCell(0).appendChild(Noticebutton.cloneNode(true));

const workingRow = document.querySelector('#cell-host');

const container = document.createElement('div');
container.appendChild(inputNotice);
container.appendChild(Noticebutton);
workingRow.insertCell(0).appendChild(container);
.column-bordered-table thead td {
  border-left: 1px solid #c3c3c3;
  border-right: 1px solid #c3c3c3;
}

.column-bordered-table td {
  border-left: 1px solid #c3c3c3;
  border-right: 1px solid #c3c3c3;
}

.column-bordered-table tfoot tr {
  border-top: 1px solid #c3c3c3;
  border-bottom: 1px solid #c3c3c3;
}
<h1>Non Working</h1>
<table id="row-host-non-working" class="column-bordered-table">
  <tr id="cell-host-non-working" style="outline: thin solid">
    <td>
      <p>Hello</p>
    </td>
  </tr>
</table>

<h1>Working</h1>
<table id="row-host-working" class="column-bordered-table">
  <tr id="cell-host" style="outline: thin solid">
    <td>
      <p>Hello</p>
    </td>
  </tr>
</table>


WYSIWYG => WHAT YOU SHOW IS WHAT YOU GET

about 4 years ago · Juan Pablo Isaza Relatório

0

Instead of inserting a new cell twice (which is the cause of your problem), do that only once and work with the reference insertCell provides:

const cell = r.insertCell(26);

const inputNotice = document.createElement("input");
inputNotice.type = "text";
const Noticebutton = document.createElement("button");
Noticebutton.type = "button";
Noticebutton.textContent = "Send Notice";

cell.append(inputNotice, Noticebutton);

Notice I'm using append here, not appendChild, because it allows to pass a number of elements to it rather than just one.

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