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 trialUche Onuekwusi
18,070 PointsWhen i click the hide list button, the function does not run. Please help
Here is the html and Javascript <!DOCTYPE html> <html> <head> <title>JavaScript and the DOM</title> <link rel="stylesheet" href="style.css"> </head> <body id = "body">
<h1 id="myHeading">JavaScript and the DOM</h1>
<button id = "toggle">hide list</button> <p>Making a web page interactive</p>
<div class = "list">
<p class = "description">Things that are purple:</p>
<input type = "text" class = "description">
<button class = "description">Change list description</button>
<ul>
<li>Pogba </li>
<li>Payet</li>
<li>Griezmann </li>
<li>Martial</li>
<li>giroud </li>
<li>lacazette</li>
<li>coman </li>
<li>mbappe</li>
<li>Dembele </li>
<li>sidibe</li>
</ul>
</div>
<script src= "bosh.js"></script>
</body> </html>
JaVAscriptcode
const input =document.querySelector("input"); const paragraph = document.querySelector("p.description"); const button3 = document.querySelector(" button.description"); const togglelist = document.getElementById("toggle"); const listDiv = document.getElementsByClassName("list");
button3.addEventListener("click", () =>{ for (let i = 0; i < playerList.length; i+=1) { playerList[i].innerHTML= input.value;
}
} );
togglelist.addEventListener("click", () => { listDiv.style.diplay = "none";
});
1 Answer
Daniel Landon Jr
14,733 PointsgetElementsByClassName returns an array. You are not accessing the array object in your code listDiv[x];
try this
togglelist.addEventListener("click", () => { listDiv[0].style.diplay = "none"; } );
A couple of suggestions.
Your formatting here was especially hard to read, I don't think I missed anything but it is very possible.
Also, you are moving back and forth between querySelector and getElementByX ... I would recommend that you go with one or the other. While neither one (query selectors or get elements by X) is better than the other the jumping back and forth can get confusing.