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 Styling Elements

Alanis Chua
Alanis Chua
2,830 Points

styling 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>    
    <button id="toggleList">Hide List</button>
    <div class="list">
      <p class='description'>Things that are purple:</p>
      <input type="text" class="description">
      <button class="description">Change list</button>
      <ul>
        <li>grapes</li>
        <li>amethyst</li>
        <li>lavender</li>
        <li>plums</li>
      </ul>
    </div>
    <script src="app.js"></script>
  </body>
</html>
const toggleList = document.getElementById('toggleList')
const listDiv = document.querySelector('.list')
const input = document.querySelector('input')
const p = document.querySelector('p.description')
const button = document.querySelector('button')

toggleList.addEventListener('click', () => {
  if(listDiv.style.display == 'none'){
    toggleList.textContent = 'Hide List';
    listDiv.style.display ='block';
  } else{
    toggleList.textContent = 'Show List';
    listDiv.style.display = 'none';
  }
  });

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

i understand that the colon will show on the screen after clicking the hide because two buttons are firing due to the button is not being assigned probably. however, i have a question here.. after keying an input and why isn't the 'change list' button function to replace the p.description? as the button.addEventListener is refering to both the button, it should be working right? why the p.description is changed when the hide button is clicked?

2 Answers

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Because you told it to - haha

You're also missing semicolons to end your statements.

// the statement below selects all buttons - so any button click executes it
const button = document.querySelector('button');
Alanis Chua
Alanis Chua
2,830 Points

thanks for the reminder on semicolon.. i still dont get it.. why the change list button wont work after i keying input and click the button?

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Good point - this community stuff really helps the understanding. I was also wondering the same thing...

I think my answer above is incomplete. Also, I think there are two key concepts here (I learning this stuff also...)

  1. The addEventListener binds to a single element - and in your example both events are attached to your toggleList element (maybe because that button is first in the html???). So, there isn't a event listener on your second button.
  2. The bubbling concept, where the event bubbles up the ancestor tree of the element. So, if you want 1 event listener attached to multiple elements then you would need to attach the event listener to a parent of all the desired buttons. In the case of your example it would be <body>. Otherwise you need to select the second button with more specificity.

Does that make any sense?

Steve Gallant
Steve Gallant
14,943 Points

At 8:05 or so in the video, Gil explains that the querySelector method only selects the FIRST instance of whatever element you're targeting. After adding the toggleList button higher in the index.html, that button now becomes the first instance and is selected by the variable declaration. So both event handlers are triggered by the same button click (of the toggleList button). Hence, adding the .description class to the variable declaration makes it more specific and ensures each button is targeted separately.