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

Problem with "myList" variable.

Hello there,

I have problem to define the variable "myList" by console.

I give the command

const myList = document.getElementsByTagName("li")

but when I try to see the variable with

myList[0];

the console give me back an error

"myList is not defined"

why?

I paste also the HTML part

<html>
  <head>
    <title>JavaScript and the DOM</title>
    <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
    <h1 id="myHeading">JavaScript and the DOM</h1>
    <p>Making a web page interactive</p>
    <hr>
    <p>Things that are purple:</p>
      <ul>
        <li>grapes</li>
        <li>amethyst</li>
        <li>lavender</li>
        <li>plums</li>
      </ul>
    <script src="app.js"></script>
  </body>

thanks!

2 Answers

Can I ask why you are using const over var as i dont believe you need to use const

var x = document.getElementsByTagName("LI");

This page should help: https://www.w3schools.com/jsref/met_document_getelementsbytagname.asp

because in this case I don't thing that can influence the code.

Cosimo Scarpa:

Remember to watch what you type. It should be "document.getElementByTagName()", not "document.getElementsByTagName()". You're getting only 1 element, not multiple elements.

var for variable isn't used any longer it's now let & const per this lesson.