Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

gael blanchemain
seal-mask
.a{fill-rule:evenodd;}techdegree
gael blanchemain
Full Stack JavaScript Techdegree Student 8,843 Points

Stuck with 'Uncaught TypeError: Cannot set property 'innerHTML' of null'

Hi, I am stuck with an error of type: 'Uncaught TypeError: Cannot set property 'innerHTML' of null'. At this point I feel I tried pretty much everything to no avail, if someone could help me, that would save my day!

Here is my current code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Random Quotes</title>
    <link rel="stylesheet" href="css/normalize.css">
    <link rel="stylesheet" href="css/styles.css">
  </head>
  <body>
    <header>
      <h1>Random Quotes</h1>
      <button id="load-quote" class="load-quote">Show another quote</button>
    </header>
    <div class="container">
      <div id="quote-box" class="quote-box">
        <p class="quote">Every great developer you know got there by solving problems they were unqualified to solve until they actually did it.</p>
        <p class="source">Patrick McKenzie<span class="citation">Twitter</span><span class="year">2016</span></p>
      </div>
    </div>
    <script src="js/script.js"></script>
  </body>
</html>```

---------------------------------------------------------------------------------------------------------

JS:

```js
const quotes = [
{
quote: `We are always in transition. If you can just relax with that, you'll have no problem.`,
source: `Chogyam Trungpa`,
citation: `azquotes.com`,
year: `1977`
},
{ 
quote: `I just can't listen to any more Wagner, you know...I'm starting to get the urge to 
conquer Poland.`,
source: `Woody Allen`,
citation: `azquotes.com`,
year: `1977`
}
]

let selectedQuote;
let numberOfQuotes;

const randomQuote = function getRandomQuote(quotes) {
 numberOfQuotes = quotes.length;
 selectedQuote = quotes[Math.floor(Math.random()* numberOfQuotes )];
  return selectedQuote;
}

randomQuote(quotes);

document.querySelector(".quote").innerHTML = selectedQuote.quote;
document.querySelector(".source").innerHTML = selectedQuote.source;
document.querySelector(".citation").innerHTML = selectedQuote.citation;
document.querySelector(".year").innerHTML = selectedQuote.year;

2 Answers

Cameron Childres
Cameron Childres
11,820 Points

Hi Gael,

When you change the value of the innerHTML property it changes everything between the closing and opening tag of the element. Take a look at the original HTML of your paragraph element with the class "source":

<p class="source">Patrick McKenzie<span class="citation">Twitter</span><span class="year">2016</span></p>

And then the last 3 lines of your JavaScript:

document.querySelector(".source").innerHTML = selectedQuote.source;
document.querySelector(".citation").innerHTML = selectedQuote.citation;
document.querySelector(".year").innerHTML = selectedQuote.year;

Once the innerHTML of the ".source" paragraph is set to selectedQuote.source it replaces EVERYTHING inside the tags with one of the sources, like this:

<p class="source">Woody Allen</p>

This gets rid of the span elements and is responsible for the error you're seeing. There's no longer anything for the last two lines of your code to work with.

Hopefully this helps nudge you in the right direction -- let me know if you need any further help!

The span related classes are not showing up for some reason. The error is being thrown on the citation class and neither it or year are displayed. Perhaps a css display issue?