Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
Clever Programmer Javascript tutorial Age in Days Challenge

For the age in Days challenge in this video, I'm getting the following error:

Uncaught TypeError: Cannot read properties of null (reading 'appendChild') at yearBorn (script.js:10:47) at        HTMLButtonElement.onclick (challenge.html:19:64) yearBorn @ script.js:10 onclick @ challenge.html:19

      

JS:

function yearBorn() {

var birthYear = prompt('What year were you born?');
var ageInDays = (2022 - birthYear) \* 365;
var h1 = document.createElement('h1');    
var textAnswer = document.createTextNode('You are ' + ageInDays + ' days old');
h1.setAttribute('id', 'yearBorn');
h1.appendChild(textAnswer);
document.getElementById('flex-box-result').appendChild(h1);

html:

<html lang="en">
  <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" />
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
    />

    <title>Javascript Challenge</title>
    <link rel="stylesheet" href="static/css/index.css"
  </head>
  <body>
    <div class="container-1">
      <h2>Challenge 1: Your Age in Days</h2>
      <div class="flex-box-container-1">
        <div>
          <button class="btn btn-primary" onclick="yearBorn()">Click Me</button>
        </div>

        <div>
          <button class="btn btn-danger">Reset</button>
        </div>
      </div>
      <div class="flex-box-container-1">
        <div class="flex-box-result"></div>
      </div>
    </div>
    <script src="static/js/script.js"></script>
  </body>
</html>

Why am I getting the error?

I was expecting the age in days to print to the flex box container below the buttons in the document. Instead, getting the error

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Solution 1 The error in this line

document.getElementById('flex-box-result').appendChild(h1)

Because document.getElementById() need an id which you provide when creating html element like this

<div id="id"></div>

Your flex-box-result is a class

<div class="flex-box-result"></div>

to

<div class="flex-box-result"></div>

Solution 2 In JavaScript code, you can also use a querySelector method with the selector of your element (like CSS selector)

document.querySelector('.flex-box-result').appendChild(h1)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda