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 trialammarkhan
Front End Web Development Techdegree Student 21,661 PointsClassName not being removed
I am trying my own code with the video but my class doesn't get removed to an empty string.
const btn = document.getElementById("btn");
const ul = document.getElementById("list");
btn.addEventListener("click", function(){
const li = document.createElement("li");
li.textContent = "Random";
ul.appendChild(li);
const label = document.createElement("label");
label.textContent = "Confirmed";
li.appendChild(label);
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
li.appendChild(checkbox);
})
ul.addEventListener('change',function(e){
const check = event.target;
const checked = check.checked;
if(checked){
const localLI = check.parentNode;
localLI.className = "confirmed"
}else{
localLI.className = "";
}
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<button id="btn">Click me</button>
<ul id="list">
</ul>
<script src="app.js"></script>
</body>
</html>
ul{
display:flex;
}
ul, li{
list-style:none;
}
.confirmed{
border-radius:5px;
border:1px solid red;
}
1 Answer
KRIS NIKOLAISEN
54,972 PointsYou have defined localLI in your if condition but then use it in the else condition. Move it above the if statement and you should be good.
const localLI = check.parentNode;
if(checked){
localLI.className = "confirmed"
}else{
localLI.className = "";
}