JavaScript JavaScript and the DOM Making Changes to the DOM Styling Elements

Hamzah Iqbal
Hamzah Iqbal
Full Stack JavaScript Techdegree Student 2,862 Points

Why is it only hiding the first element and not all?

The code is only hiding the first element not all elements:

<!DOCTYPE html>
<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>

                          <div class="list">



    <p class="description">Things that are purple:</p>
    <button id="toggleList">Hide List</button>
    <input type="text" class="description">
    <button class="description">Change list description</button>
    <ul>
      <li>grapes</li>
      <li>amethyst</li>
      <li>lavender</li>
      <li>plums</li>
    </ul>
                            </div>


    <script src="app.js"></script>
  </body>
</html>

The div should be correct placed.

const input = document.querySelector('input');
const p = document.querySelector('p.description');
const button = document.querySelector('button');
const toggleList = document.getElementById("toggleList");
const list = document.querySelector("list");


toggleList.addEventListener("click", () => {


     list.style.display = "none";                      


                           });
button.addEventListener('click', () => {
  p.innerHTML = input.value + ':';
});

I even tried. document.querySelectorAll("list"); The only item that is hiden, is the first element
<p class="description">Things that are purple:</p>

3 Answers

Steven Parker
Steven Parker
182,024 Points

The term "list" is not a valid HTML tag name. The correct tag name for the unordered list is "ul".

Hamzah Iqbal
Hamzah Iqbal
Full Stack JavaScript Techdegree Student 2,862 Points

That doesn't solve the issue. Also in the video the class in the HTML used is = "list", but that seem to work, only in the video.

Hamzah Iqbal
Hamzah Iqbal
Full Stack JavaScript Techdegree Student 2,862 Points

I managed to solve it. It wasn't selecting the class:

list

Changed to

.list
Steven Parker
Steven Parker
182,024 Points

That would work for hiding the entire div element. I thought you were asking about how to hide all the list items.