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
Daniel Hernandez
13,437 PointsWhat is wrong with my code? When I run it, the "up" button removes the line like the remove "remove" button does.
I have looked over what the teacher did many times and still cannot seem to find out what is wrong with my code. Here is what I have for HTML
<!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 class description</button>
<ul>
<li>grapes
<button class = "up">Up</button>
<button class = "remove">Remove</button>
</li>
<li>amethyst
<button class = "up">Up</button>
<button class = "remove">Remove</button>
</li>
<li>lavender
<button class = "up">Up</button>
<button class = "remove">Remove</button>
</li>
<li>plums
<button class = "up">Up</button>
<button class = "remove">Remove</button>
</li>
</ul>
<input type="text" class = "addItemInput">
<button class = "addItemButton">Add item</button>
</div>
<script src="app.js"></script>
</body>
</html>
And here is what I have for the javascript.
const toggleList = document.getElementById('toggleList');
const listDiv = document.querySelector('.list');
const descriptioninput = document.querySelector('input.description');
const descriptionp = document.querySelector('p.description');
const descriptionbutton = document.querySelector('button.description');
const listUl = listDiv.querySelector('ul');
const addItemInput = document.querySelector('input.addItemInput');
const addItemButton = document.querySelector('button.addItemButton');
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';
}
});
descriptionbutton.addEventListener('click', () => {
descriptionp.textContent = descriptioninput.value + ':';
descriptioninput.value = "";
});
listUl.addEventListener('click', (event)=> {
if ( event.target.tagName == 'BUTTON'){
if (event.target.className = 'remove'){
let li = event.target.parentNode;
let ul = li.parentNode;
ul.removeChild(li);
}
}
if ( event.target.tagName == 'up'){
let li = event.target.parentNode;
let prevLi = li.previousElementSibling;
let ul = li.parentNode;
if(prevLi){
ul.insertBefore(li, prevLi);
}
}
});
addItemButton.addEventListener('click', () => {
let ul = document.getElementsByTagName('ul')[0];
let li = document.createElement('li');
li.textContent = addItemInput.value;
ul.appendChild(li);
addItemInput.value = "";
});
Thanks!
2 Answers
Steven Parker
243,656 PointsThe code is rather difficult to read, but I spotted a few issues:
- instead of a test (==) for "remove", the className is assigned (=) to "remove" in the "if"
- the tagName is compared to "up" but it should be the className instead
- the code that checks for "up" is outside of the test for "BUTTON" but it should be inside
For future postings, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area.
Or watch this video on code formatting.
Daniel Hernandez
13,437 PointsSorry for having my code hard to read. I quickly see the value of readability.And thanks! must be something with my browser then
Daniel Hernandez
13,437 PointsDaniel Hernandez
13,437 PointsThanks, but I still can't seem to get it to work. Can you look over again what I have in case I screwed up again. Sorry, I am quite new to this all and am still getting the hang of it. The "up" button still won't do anything
Steven Parker
243,656 PointsSteven Parker
243,656 PointsLooks good to me. I tried replacing the handler in the original code with this new snippet and it seems to work as expected. Clicking "up" moves the item up.