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 trialAlanis Chua
2,830 Pointsstyling 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
19,870 PointsBecause 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');
Steve Gallant
14,943 PointsAt 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.
Alanis Chua
2,830 PointsAlanis Chua
2,830 Pointsthanks 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
19,870 PointsDave StSomeWhere
19,870 PointsGood 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...)
Does that make any sense?