0

I've started learning js yesterday so I was writing a simple program, but I had some problems with the innerHTML function. Here the script and the exit code:

const jackShepard = {
    'name':'Jack', 
    'surname':'Shepard',
    'seasons':['1','2','3',
               '4','5','6'],
    'gender':'male',
    'is_dead': true
};

const kateAusten = {
    'name':'Kate',
    'surname':'Austen',
    'seasons':['1','2','3',
              '4','5','6'],
    'gender':'female',
    'is_dead': false 
};

function isDead(carachter){
    switch(carachter.is_dead){
        case true:
            document.getElementById('title').innerHTML = carachter.name + ' died in season ' 
                                                         + carachter.seasons[carachter.seasons.length - 1] + ' :( ';
            break; 
        case false:
            document.getElementById('subtitle').innerHTML = carachter.name + 'is still alive :)';
            break;
    }
}

isDead(jackShepard)
isDead(kateAusten)

main.js:50 Uncaught TypeError: Cannot set property 'innerHTML' of null at isDead

Andrew Morton
  • 22,953
  • 9
  • 55
  • 76
  • Hi welcome to stack overflow.. it's good to put your code inside a code block and format it so it's a bit easier for other people to read, so we can help – James McGlone May 01 '21 at 17:39
  • I've formatted your question so you can see how it can be done. – Andrew Morton May 01 '21 at 17:43
  • You are just missing the HTML for the divs with the id's you are updating the innerHTML for in your javascript. Try adding
    to your HTML before the javascript code.
    – James McGlone May 01 '21 at 17:49

0 Answers0