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

I get document is not defined error

i get the error on every time i use the document. method. i have tried the code on 2 diffrent computers i use brackets to test and tried atom, and try it on google chrome verision 61.0.3163.100

script.js
// event listener to respond to "Show another quote" button clicks
// when user clicks anywhere on the button, the "printQuote" function is called
var button = document.getElementById('loadQuote');
button.addEventListener("click", printQuote, false);





//First i make the varible random have a random number between 0 - 5
function getRandomQuote(quotes) {
    var random = Math.floor((Math.random() * quotes.length) );
    return quotes[random];


}

function printQuote() {
    var red = Math.floor(Math.random() * 256);
    var green = Math.floor(Math.random() * 256);
    var blue = Math.floor(Math.random() * 256);

    var ranrgb= "rgb("+ red + "," + green + "," + blue + ")";
    style.backgroundColor = ranrgb;

    var randomQuote = getRandomQuote(quotes);
    var html = '<p class ="quote">' + randomQuote.quote + "</p>" + '<p class = "source">' + randomQuote.source + "</p>";
    document.getElementById('quote-box').innerHTML = html;
}

//Here is a array of objects that stores qoutes and the source 

var quotes = [
    {
        quote: "But man is not made for defeat. A man can be destroyed but not defeated",
        source: "Ernest Hemingway"
    },
    {
        quote: "All that we see or seem is but a dream within a dream",
        source: "Edgar Allan Poe"
    },
    {
        quote: "There is no charm equal to tenderness of heart",
        source: "Jane Austen"
    },
    {
        quote: "Learning never exhausts the mind",
        source: "Leonardo da Vinci"
    },
    {
        quote: "If you cannot do great things, do small things in a great way",
        source: "Napoleon Hill"
    }
];
index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Random Quotes</title>
  <link href='https://fonts.googleapis.com/css?family=Playfair+Display:400,400italic,700,700italic' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" href="css/normalize.css">
  <link rel="stylesheet" href="css/styles.css">
</head>
<body>
  <div class="container">
    <div id="quote-box">
      <p class="quote">You can do anything but not everything</p>
      <p class="source">David Allen<span class="citation">Making It All Work</span><span class="year">2009</span></p>
    </div>
    <button id="loadQuote">Show another quote</button>
  </div>
  <script type="text/javascript" src="js/script.js"></script>
</body>
</html>

I can now run the website and everything works but i still get the errors that the document. errors on every document + i get the error that Printquote is defined but never used. i used in the html onclick="printQuote()"

Do

Steven Parker
Steven Parker
229,644 Points

If you have a new issue, you might want to start a new question where you can share the current code.

Also, that message sounds like a warning from your editor more than an actual error.

2 Answers

Steven Parker
Steven Parker
229,644 Points

I was not able to replicate the same error. But I did get "style is not defined" in reference to this line:

    style.backgroundColor = ranrgb;

That line looks unfinished. Did you perhaps mean something like this?

  document.getElementById("quote-box").style.backgroundColor = ranrgb;
Seth Kroger
Seth Kroger
56,413 Points

I think it's rather odd that you got document as not defined (it's supposed to be a predefined global in the browser) verses style not defined (which is the error message I'd expect you to get). What browser are you using (specific version) and what line is the error occurring at?

i get the error on every time i use the document. i have tried the code on 2 diffrent computers i use brackets to test and tried atom, and try it on google chrome verision 61.0.3163.100