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

101
Visualizações
my list function doesn't work in javascript

so i wrote this code that would create a list and then append an input to it on click. really simple. but the problem is it doesn't work and i have no idea why here is the code:

function pushing() {
  var li = document.createElement("li");
  var inputValue = document.getElementById("inp").value;
  var pushchild = document.createTextNode(inputValue);
  li.appendChild(pushchild);
}

sub.addEventListener("click", pushing);

the id inp is an input id. thank you

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

0

Your list item is never appended to a list element.

// Cache the elements,
// add the button listener,
// & focus on the input
const list = document.querySelector('ul');
const input = document.querySelector('#inp');
const sub = document.querySelector('#sub');
sub.addEventListener('click', pushing);
input.focus();

function pushing() {
  const li = document.createElement('li');
  const text = document.createTextNode(input.value);
  li.appendChild(text);

  // Adding the list item to the list element
  list.appendChild(li);
}
<input id="inp" />
<button id="sub">Click</button>
<ul></ul>

about 4 years ago · Juan Pablo Isaza Relatório

0

Add this to the last line of your function. Append your newly created li element to the ul.

document.querySelectorAll(‘ul’).appendChild(newCreatedLi);
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