jeudi 24 septembre 2020

getElementById is not working if a checkbox's value is passed as variable

Here is JS function. this function is an event handler for a checkbox. so the element is the checkbox.

function updateSelect(element) {
        // this works
        document.getElementById("file_update_" + 1).disabled = !element.checked;
        // this does not works. the element.value is also 1
        document.getElementById("file_update_" + element.value).disabled = !element.checked;
}

the console has the following error.

Uncaught TypeError: Cannot set property 'disabled' of null
    at updateSelect (<anonymous>:21:75)
    at HTMLInputElement.onchange (updateuser:1)

any idea....what is unique about the element.value?




Aucun commentaire:

Enregistrer un commentaire