0

I want to create a script which enters some data from a JSON file in some fields.

It has to click on a button to select the proper form. After the click, the new form will pop up without changing the page (the page is dynamically updated) - so new content is inserted into the DOM, but my function will throw an error - "document.getElementById is null".

Why is that and how can I fix it?

I've tried with using setTimeout/setInterval (so my function waits a bit for the content to load into the DOM - but it still doesn't work.)

This is my code:

var JSONDeBagat = 
{
    "chelt": [
        {
            "Partener": "S",
            "cofinatare %": "5",
            "Activitate": "A.6.1",
            "Functie": "Manager de proiect",
            "Tip expert": "manager proiect -  sub 5 ani",
            "Denumire de bagat in my smis": "Salariu net - Manager de proiect - manager proiect -  sub 5 ani - total ore alocate: 2016",
            "Categorie Smys": "Cheltuieli aferente managementului de proiect",
            "Subcategorie Smys": "23-Cheltuieli salariale cu managerul de proiect",
            "Tip cheltuiala": "Salariu net",
            "Luni": "24",
            "Ore pe zi": "4",
            "Total ore": "2016",
            "Tarif orar brut": "60.00",
            "Total cheltuiala": "120960.00",
            "Nerambursabil": "114912.00",
            "Cofinantare": "6048.00",
            "Justificare": "Salariu net - Expert tip manager proiect -  sub 5 ani - implicat circa 24 cu o repartizare a muncii inegala, cu un total de 2016 ore, echivalent la un CIM cu o norma de lucru de 4 ore/zi."
        }
    ]
}

JSONDeBagat.chelt.forEach(element => {
    var decizieActivitate = element.Activitate.slice(2);
    var stringButton = "bugetFormTree:treeTableBuget:" + (decizieActivitate[0] - 1) + "_" + (decizieActivitate[2] - 1) + ":j_idt795";
    document.getElementById(stringButton).click();
    setTimeout(completareValori(element), 3000);
});

function completareValori(element)
{
    document.getElementById("formCheltuialaBuget:denumire").value = element["Total cheltuiala"];
    console.log(element["Total cheltuiala"]);
    document.getElementById("formCheltuialaBuget:cat").value = 25;
    if(element["Subcategorie Smys"].slice(0, 3).includes("-"))
    {
        document.getElementById("formCheltuialaBuget:subcat").value = element["Subcategorie Smys"].slice(0, 2);
    }
    else
    {
        document.getElementById("formCheltuialaBuget:subcat").value = element["Subcategorie Smys"].slice(0, 3);
    }
    document.getElementById("formCheltuialaBuget:um").value = "ore";
    document.getElementById("formCheltuialaBuget:cantitate").value = element["Total ore"];
    document.getElementById("formCheltuialaBuget:pretUnitar_input").value = element["Tarif orar brut"];
    document.getElementById("formCheltuialaBuget:j_idt1186_input").value = 0;
    document.getElementById("formCheltuialaBuget:j_idt1221").value = element["Justificare"];
    document.getElementById("formCheltuialaBuget:j_idt1237_input").value = element.Nerambursabil;
}
  • Do you really have element IDs with `:` in them? That's a bad idea, since that character has special meaning in CSS selectors. – Barmar Feb 22 '21 at 22:57
  • @Barmar unfortunately, yes. The website is not mine, I'm just creating this script to only upload data faster :) – Octavian Niculescu Feb 22 '21 at 22:58

0 Answers0