Uncaught Typeerror: Cannot read Property 'checked' of null

Asked

Viewed 1,223 times

-3

I am having this error in my code: "Uncaught Typeerror: Cannot read Property 'checked' of null", I searched for it in Google, tried the presented solutions but none worked.

cadastro-cuidador.js:

function HabilitarUpload() {
    if (document.getElementById(declaracaoCuidador).checked) {
        document.getElementById(inputCertificado).removeAttribute("hidden");
    } else {
        document.getElementById(inputCertificado).setAttribute("hidden", "true");
    }
}

index.html:

<!DOCTYPE html>
    <html lang="pt-br">
    <head>
        <title>Cuidadores</title>
        <meta charset="UTF-8">
        <script language="JavaScript" src="./js/cadastro-cuidador.js" defer></script>
    </head>
    <body>
        <div class="input-cuidador">
            <p>
                <label class="container">Sou um cuidador
                <input type="checkbox" id="declaracaoCuidador" onclick="HabilitarUpload()">
                <span class="checkmark"></span>
                </label>
            </p>
            <p id="pInputCertificado" hidden="true">Insira seu certificado (.pdf, .png ou .jpg)
            <input type="file" name="img-certificado" id="inputCertificado"></p>
        </div>
    </body>
</html>

I tried to put the script at the end of the body, but it didn’t solve.

1 answer

0


You just forgot to put the ids you are searching between quotes:

function HabilitarUpload() {
    if (document.getElementById('declaracaoCuidador').checked) {
        document.getElementById('inputCertificado').removeAttribute("hidden");
    } else {
        document.getElementById('inputCertificado').setAttribute("hidden", "true");
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.