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

158
Visualizações
Input text not being displayed on Html Page

I and trying to make a basic web page as a project and can not get my text to display for a label tag being injected using javaScript. If anyone has any ideas or could point me in the right direction that would be great. Here is a snippet of the code I am having trouble with.

let todo = [
  {
    text: "Item 1",
    completed: true,
  },
  {
    text: "Item 2",
    completed: false,
  },
  {
    text: "Item 3",
    completed: false,
  },
  {
    text: "Item 4",
    completed: false,
  },
  {
    text: "Item 5",
    completed: true,
  },
];
todo.forEach(function (todos, index) {
  const p = document.createElement("p");
  const div = document.createElement("div");
  const checkBox = document.createElement("input");
  checkBox.type = "checkbox";
  checkBox.id = `newTodo${index}`;
  checkBox.value = `todoItem${index}`;
  const checkBoxLabel = document.createElement("label");
  checkBoxLabel.htmlFor = `todoItem${index}`;
  checkBoxLabel.innerText = todos.text; //"This is the text that is not being rendered to the browser"
 
  document
    .querySelector("#todoItem")
    .appendChild(div)
    .appendChild(checkBox)
    .appendChild(checkBoxLabel);
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your code is actually working. But its generating the labels inside the checkbox input. Thats why you are not able to see it.

The issue is with the below section

document
    .querySelector("#todoItem")
    .appendChild(div)
    .appendChild(checkBox)
    .appendChild(checkBoxLabel)

What this does?

This will first select an element from the DOM having id todoItem. Inside that your div will be appended. Inside that div, the checkBox which is the input, will get appended. Inside that input checkBoxLabel which is the label will be appended.

So your heirarchy will be like, div inside the container #todoItem, checkBox inside that div, checkBoxLabel inside checkBox.

You were appending checkBoxLabel inside the checkbox. It should be in the same level of checkbox OR the input can be placed inside the label. A label inside a checkbox will not be visible. You can either place the checkbox inside the label OR both checkbox and label in same hierarchy inside div.

Placing checkbox inside label

document
  .querySelector("#todoItem")
  .appendChild(div)
  .appendChild(checkBoxLabel)
  .appendChild(checkBox);

This will make the checkbox to be placed after the label. like the one below

enter image description here

Placing checkbox and label inside the div with same hierarchy.

div
  .appendChild(checkBox);
div
  .appendChild(checkBoxLabel);

document
  .querySelector("#todoItem")
  .appendChild(div);

This will display the checkbox first, then the label like below

enter image description here

Please find the working fiddle with the second example.

const todo = [
  { text: "Item 1", completed: true, },
  { text: "Item 2", completed: false, },
  { text: "Item 3", completed: false, },
  { text: "Item 4", completed: false, },
  { text: "Item 5", completed: true, },
];
todo.forEach(function (todos, index) {
  const p = document.createElement("p");
  const div = document.createElement("div");

  const checkBox = document.createElement("input");
  checkBox.type = "checkbox";
  checkBox.id = `newTodo${index}`;
  checkBox.value = `todoItem${index}`;

  // added checking condition
  checkBox.checked = todos.completed;

  const checkBoxLabel = document.createElement("label");
  checkBoxLabel.htmlFor = `todoItem${index}`;
  checkBoxLabel.innerText = todos.text;

  // Place checkbox first then the label, Both on the div
  div
    .appendChild(checkBox);
  div
    .appendChild(checkBoxLabel);

  document
    .querySelector("#todoItem")
    .appendChild(div);
});
<div id="todoItem"></div>

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