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 trialMuaaz Matwadia
Full Stack JavaScript Techdegree Graduate 19,327 PointsTo Do List
How do a get the clear button to clear all items in the list
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- displays site properly based on user's device -->
<link href="style.css" rel="stylesheet">
<link rel="icon" type="image/png" sizes="32x32" href="./images/favicon-32x32.png">
<title>To Do List</title>
</head>
<body>
<header>
<h1>ToDo</h1>
<img src='images/icon-sun.svg' alt="sun for light mode" id="sun">
<form>
<input type="text" placeholder="Create a new todo..." name="listItem" id='create'></input>
</header>
<ul id='list'></ul>
<!-- <img src="images/icon-cross.svg" alt='cross to delete list item'> -->
<div class="buttons">
<button>All</button>
<button>Active</button>
<button>Completed</button>
</div>
<button id="clear">Clear</button>
</form>
<div class="attribution">
Coded by <a href="#">Muaaz Matwadia</a>.
</div>
<script src="script.js"></script>
</body>
</html>
}
```CSS{
body{
display:flex;
flex-direction: column;
justify-self: center;
align-items: center;
/* background-color: rgb(228, 227, 227); */
background-color: #e5e5e5;
}
header{
background: url('images/bg-mobile-light.jpg') ;
background-size: cover;
padding: 40px;
}
form{
box-shadow: 0 4px 10px rgba(0, 0,0, 0.1);
width: 300px;
}
h1{
font-size: 2rem;
font-family: Arial, Helvetica, sans-serif;
color:#ffffff;
display: inline;
margin-right: 80px;
margin-bottom: 30px;
}
input{
width: 80%;
margin: 30px;
}
input:focus{
outline-color: blueviolet;
}
#create{
display:block;
padding: 15px;
margin: 10px;
}
.buttons{
background-color: #ffffff;
padding: 15px;
border-radius: 6px;
}
a{
margin: 15px;
}
}
```js{
const form = document.querySelector('form');
const createListItem = document.querySelector('input');
const ul = document.getElementById('list');
createListItem.focus()
form.addEventListener('submit' , (e) =>{
e.preventDefault();
let newItem = createListItem.value;
const li = document.createElement('li');
li.textContent = newItem ;
ul.appendChild(li);
createListItem.value = '';
const check = document.createElement('input');
check.type = 'checkbox';
check.className = "checkboxes";
ul.appendChild(check);
const liImg = document.createElement('img') ;
liImg.src = 'images/icon-cross.svg';
ul.appendChild(liImg);
liImg.addEventListener('click' ,(e) => {
ul.remove('li')
});
});
const clearAll = document.getElementById('clear')
}
1 Answer
Jassim Alhatem
20,883 PointsHi Azzie Fuzzie,
There many ways but this is the simplest:
const list = document.querySelector('#list')
function clearAll(){
list.innerHTML = "";
}
btn.addEventListener('click', clearAll);