mercredi 6 mai 2020

How to give a javascript made checkbox an onchange event?

Hey I'm trying to create a few checkboxes in javascript and add them to my html. I want to give the checkboxes an onchange event but the event only fires when they get created and when the site is loaded and I click on the checkboxes nothing happens. As well if I look into the source code of the site in the browser there is no onchange event at the checkboxes.

hashtags is an Array with objects with some properties but they aren't important

hashtags.forEach(element => {
    if(element.categorie == searchedCategorie && element.beitrage == searchedSize && element.language == searchedLang){
        searchedHashtags.push(element.hashtagName);
        var label = document.createElement("label");
        var checkbox = document.createElement("input");
        checkbox.className = "checkboxes";
        checkbox.type = "checkbox";
        checkbox.value = element.hashtagName;
        checkbox.onchange = handleChange(checkbox);
        checkbox.name = element.hashtagName;
        label.appendChild(checkbox);
        label.innerHTML += element.hashtagName;
        body.appendChild(label);
        body.appendChild(document.createElement("br"));
    }
});



function handleChange(checkbox){
if(checkbox.checked == true){
    countChecked++;
}else if(checkbox.checked == false){
    countChecked--;
}
txtarea.innerHTML = countChecked + "/" + searchedHashtags.length + " ausgewählt";
alert(countChecked);

}




Aucun commentaire:

Enregistrer un commentaire