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 trialHarris Keka
Courses Plus Student 3,611 PointsMy code does not append child, instead it replaces the first child. I've checked my code and can't figure out. Help Pls.
let updateButton = document.querySelector('button.update');
let updateHeadingInput = document.querySelector('input.updateHeading');
let updateDescriptionInput = document.querySelector('input.updateDescription');
let theHeading = document.getElementById('myHeading');
let headingDescription = document.getElementById('headingDescription');
let p = document.querySelector('p.description')
let hideListItemsButton = document.querySelector('button.hideListItems');
let listItems = document.querySelector('div.listItems');
let addListItem = document.querySelector('button.addListItem');
let addListItemButton = document.querySelector('button.addListItemButton');
let addListItemInput = document.querySelector('input.addListItemInput');
//Add eventlistiner to handle on click.
updateButton.addEventListener('click', () => {
theHeading.textContent = updateHeadingInput.value;
headingDescription.textContent = updateDescriptionInput.value;
updateDescriptionInput.value = '';
});
hideListItemsButton.addEventListener('click', () =>{
if(listItems.style.display == "block") {
hideListItemsButton.textContent = "Show List";
listItems.style.display = "none";
}else{
hideListItemsButton.textContent = "Hide List";
listItems.style.display = "block";
}
});
addListItemButton.addEventListener('click', () => {
let ul = document.getElementsByTagName('ul')[0];
let li = document.querySelector('li');
li.textContent = addListItemInput.value;
ul.appendChild(li);
addListItemInput.value = '';
}); ```
1 Answer
Steven Parker
231,141 PointsYes, here's the difference:
let li = document.querySelector('li'); // to replace the first element
let li = document.createElement("li"); // to add a new one at the end
Harris Keka
Courses Plus Student 3,611 PointsHarris Keka
Courses Plus Student 3,611 PointsThink I got the error, I did selected an <li> and stored in the li variable instead of creating a new li element.