Skip to content
Permalink
523544c01c
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
99 lines (80 sloc) 2.43 KB
<!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>My Shopping List App</title>
<style>
#item{
border: 1px solid #ccc;
font-size: 16px;
}
ul,li {
margin: 0px;
padding: 0px;
list-style-type: none;
}
li{
padding: 0.5em;
}
input:checked + span{
text-decoration: line-through;
}
</style>
</head>
<body>
<h1>My Shopping List App</h1>
<input type="text" name="item" id="item"> <button id="add">Add</button>
<hr>
<ul id="shopping-list">
</ul>
<script>
const addBtn = document.getElementById('add');
const inputBox = document.getElementById('item');
const list = document.getElementById('shopping-list');
let initItems = ['Milk', 'Cookies'];
let allItems = [];
addBtn.addEventListener('click', addItem);
function arrayRemove(arr, value) {
return arr.filter(function(ele){
return ele != value;
});
}
function addItem(value){
if(typeof value == 'string'){
item = value;
}
else{
item = inputBox.value;
}
let li = document.createElement('li');
li.className = "list-item";
let input = document.createElement('input');
input.type = "checkbox";
input.name = "items";
let span = document.createElement('span');
span.innerText = item;
let a = document.createElement('a');
a.innerText = "x";
a.href = "";
a.addEventListener('click',function(event){
event.preventDefault();
this.parentNode.remove();
allItems = arrayRemove(allItems, this.previousSibling.innerText);
});
li.appendChild(input);
li.appendChild(span);
li.appendChild(a);
allItems.push(item);
list.appendChild(li);
inputBox.value = "";
inputBox.focus();
console.log(allItems);
}
initItems.forEach(element => {
addItem(element);
});
</script>
</body>
</html>