jeudi 3 juin 2021

.checked selhává na error: Uncaught TypeError: Cannot read property 'checked' of null [duplicate]

I need to detect a checkbox change and then write in the variable whether it is checked true or false, but to give it it returns an error: .checked fails on error: Uncaught TypeError: Cannot read property 'checked' of null. and I don't know what to do anymore.

fnction change() {
let x = document.getElementById("myid").checked
}

console.log(x)
<!DOCTYPE html>
<head>
<html lang="cz">
<meta charset="UTF-8">
<title>Page Title</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<body>
<input type="checkbox" id="myid" class="cb" onchange="change()">

<script src="script.js"></script>
</body>
</html>



Aucun commentaire:

Enregistrer un commentaire