#forEach Help

8 messages · Page 1 of 1 (latest)

tardy quest
#

The easiest way is to never use forEach. Show code

pure vortex
#

Hehe I'm all new to js soo


function bloodDna() {
    fetch(`https://valdemar_nuiComputer/indhentBlodData`, {
        method: 'POST',
    })
    

        const fjern = document.getElementById("blod");
        fjern.style.display = "block";


    window.addEventListener('message', (event) => {
        var information = event.data.type
        var dataT = JSON.parse(information);

        var sog = document.getElementById("startSøgning");
        sog.style.display = "none";

            dataT.forEach(betjent => {

                const accordion = document.getElementById("accordionFlushExample");
                const accordionItem = document.createElement("div")
                accordionItem.classList.add("accordion-item");
                accordionItem.classList.add(`item`)
                accordion.appendChild(accordionItem); 

                const accordionHeader = document.createElement("h2");
                accordionHeader.classList.add("accordion-header");
                accordionHeader.id = `flush-heading${betjent.id}`;
                accordionItem.appendChild(accordionHeader);


                const headerButton = document.createElement("button");
                headerButton.classList.add("accordion-button", "collapsed");
                headerButton.setAttribute("type", "button");
                headerButton.setAttribute("data-bs-toggle", "collapse");
                headerButton.setAttribute("data-bs-target", `#flush-collapse${betjent.id}`);
                headerButton.setAttribute("aria-expanded", "false");
                headerButton.setAttribute("accordion-color", "false");
                headerButton.setAttribute("aria-controls", `flush-collapse${betjent.id}`);
                headerButton.innerText = `🟢Oprettet Af: ${betjent.betjent} | Sagsnummer: #${betjent.id} | Navn: ${betjent.fornavn} `;
                accordionHeader.appendChild(headerButton);


                const accordionCollapse = document.createElement("div");
                accordionCollapse.id = `flush-collapse${betjent.id}`;
                accordionCollapse.classList.add("accordion-collapse", "collapse");
                accordionCollapse.setAttribute("aria-labelledby", `flush-heading${betjent.id}`);
                accordionCollapse.setAttribute("data-bs-parent", "#accordionFlushExample");
                accordionItem.appendChild(accordionCollapse);



                const accordionBody = document.createElement("div");
                accordionBody.classList.add("accordion-body");
                accordionBody.innerHTML = `<p><strong>DATO:</strong><br>${betjent.dato}</p><p><strong>PALCERING:</strong><br>${betjent.placering}</p><p><strong>Date Fra Analysen:</strong><p><strong>Navn: </strong>${betjent.fornavn}<p><strong>Køn: </strong>${betjent.køn}</p><p><strong>Information:</strong><br>${betjent.information}</p><button type="button" class="btn btn-danger" onclick="sletRapport('${betjent.id}')">Slet Rapport</button>`
                accordionCollapse.appendChild(accordionBody);

            });
            /*globalThis.loopBlodTrykket = true;*/
        
    });
}
tardy quest
#

how many times is bloodDna called?

pure vortex
#

I gets called everytime i press a button

tardy quest
#

Right

#

so you add a new listener to window at every button press

#

first click you have 1 listener, forEach is executed once. Second click you add another time, you now have 2 equal listeners, forEach is executed twice

pure vortex
#

Okay il try that out thanks 😄