Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!