<>
Ola a todos, estou a duas semanas com erro esse seguinte erro:
Uncaught TypeError: Cannot read properties of null (reading 'value')
at verificar (script.js:6:13)
at HTMLInputElement.onclick (EXERCICIOS1.HTML:27:64)
estou fazendo um verificador de idade, usando JS E HTML
HTML:
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> Verificador de idade</title>
<link rel="stylesheet" href="estilo.css">
</head>
<body>
<header>
<h1> Verificador de idade</h1>
</header>
<section>
<div>
<p>
Ano de nascimento?
<input type="number" name="txtano" id="textano" min="0">
</p>
<p>Sexo?
<input type="radio" name="radsex" id="mas" checked>
<label for="mas">Masculino</label>
<input type="radio" name="radsex" id="fem">
<label for="fem">Feminino</label>
</p>
<p>
<input type="button" value="verificar" onclick="verificar()">
</p>
</div>
<div id = "res">
Preencha os dados acima para ver o resultado
</div>
</section>
<footer>
<p>©CursoemVideo </p>
</footer>
<script src="script.js"></script>
</body>
</html>
JS:
function verificar(){
var data = new Date()
var ano = data.getFullYear()
var fano = document.querySelector('#txtano')
var res = document.querySelector('div#res')
if (fano.value.length == 0 ||fano.value > ano) {
window.alert('[ERRO] Verifique os dados e tente novamente')
}else{
window.alert('tudo ok')
}
}
HTML:
<!DOCTYPE html>
<html lang="pt-BR">
JS:
function verificar(){
var data = new Date()
var ano = data.getFullYear()
var fano = document.querySelector('#txtano')
var res = document.querySelector('div#res')
if (fano.value.length == 0 ||fano.value > ano) {
window.alert('[ERRO] Verifique os dados e tente novamente')
}else{
window.alert('tudo ok')
}
}
<>