Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!