Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

254
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda