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

258
Visualizações
document.getElementById(idz.toString()) causing Type Error: typ1 is null javascript

I am trying to add a check box when button is pressed and also delete the same checkbox when the delete is pressed. Unfortunatly the delete function cannot run as it seems that the newly created div doesn't exist yet. How can I get around this?

<script>
    const list = document.getElementById("list");
    const cli_form = document.getElementById("client-form");
    let idz = 0;

    function add_client(input) {

        const div = document.createElement("div");
        div.setAttribute("id", idz.toString())


        const newCheckbox = document.createElement("input");
        newCheckbox.setAttribute("type", 'checkbox');
        newCheckbox.setAttribute("id", 'checkbox');

        const newLabel = document.createElement("label");
        newLabel.setAttribute("for", 'checkbox');
        newLabel.innerHTML = input.value;

        const br = document.createElement("br");

        const del = document.createElement("input");
        del.setAttribute("type", 'button');
        del.setAttribute("value", idz.toString());
        del.onclick = delete_item(document.getElementById(idz.toString()));

        div.appendChild(newCheckbox);
        div.appendChild(newLabel);
        div.appendChild(del);
        div.appendChild(br);

        list.appendChild(div);
        idz++;
    }

    function delete_item(item1) {
        item1.remove();
    }
</script>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are assigning the result of the function delete_item which invoke immediately. Therefore, it is removing the item before it is being added to DOM. You might use an arrow wrapper to avoid immediate invocation. Like this :

del.onclick = () => delete_item(document.getElementById(idz.toString()));
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