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 JavaScript and the DOM (Retiring) Making Changes to the DOM Removing an Element from the DOM

sohaib ahmad
sohaib ahmad
2,877 Points

Select the unordered list element and store it in the variable myList stuck in this challenge please help.

Select the unordered list element and store it in the variable myList ...need help in this question

index.html
<!DOCTYPE html>
<html>
    <head>
        <title>DOM Manipulation</title>
    </head>
    <link rel="stylesheet" href="style.css" />
    <body>
        <ul>
            <li id="first">First Item</li>
            <li id="second">Second Item</li>
            <li id="third">Third Item</li>
        </ul>
        <script src="app.js"></script>
    </body>
</html>
app.js
let myList =document.getElementsByTagName('li').length;
let firstListItem;

7 Answers

Brian Foley
Brian Foley
8,440 Points

let myList = document.getElementsByTagName('ul')[0];

Thank you, God bless you and your family.

Steven Parker
Steven Parker
229,788 Points

:point_right: It looks like you're selecting the wrong item.

Task 1 asks you to "Select the unordered list element and store it in the variable myList". An unordered list would have a tag of "ul".

Also, you won't need the length property, but if you select an item using getElementsByTagName you will need to add a subscript (using []'s and an index) to convert the collection it returns into a single item. As an alternative you could also select it using querySelector, which returns only the first matching item.

Select the unordered list element and store it in the variable myList is : myList = document.querySelector("ul");

firas fares
PLUS
firas fares
Courses Plus Student 2,073 Points

let myList ; myList =document.querySelector("ul");

Liang Huang
Liang Huang
5,178 Points

let myList = document.getElementsByTagName('ul')[0]; let firstListItem = document.querySelector('li:first-child');

Greg Schudel
Greg Schudel
4,090 Points

I am confused about the how and where techniques for using methods...

1.) When should you use quotations within a method to call an element? For example, I've seen this and was told it's okay

ul.removeChild(li);

but clearly the above is fine:

myList = document.querySelector("ul");

2.) When should you use double quotations versus single quotations for these methods?

3.) Does anyone have any reference that shows you when you should use one method over another? After using them all they seem despairingly similar. For example, couldn't you use

let myList = getElementByTagName('ul');

for the above question. But what about

let myList = document.querySelectorAll('ul').[0];

Is this a subjective question? If so, I apologize. I never know if what I ask is subjective until I ask! LOL

let myList = document.querySelector('ul'); let firstListItem = document.querySelector('#first');