Visitar URL original
add toggleCheck to #15 · YuriBrunetto/JavaScript30@7bce535 · GitHub
Skip to content

Commit 7bce535

Browse files
committed
add toggleCheck to wesbos#15
1 parent 0ea18be commit 7bce535

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

‎15 - LocalStorage/index-START.html‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@ <h2>LOCAL TAPAS</h2>
2222
<form class="add-items">
2323
<input type="text" name="item" placeholder="Item Name" required>
2424
<input type="submit" value="+ Add Item">
25+
<div class="clear-both"></div>
26+
<button type="button" id="btn-check">Check all</button>
27+
<button type="button" id="btn-uncheck">Uncheck all</button>
28+
<div class="clear-both"></div>
2529
</form>
2630
</div>
2731

2832
<script>
2933
const addItems = document.querySelector('.add-items')
3034
const itemsList = document.querySelector('.plates')
3135
const items = JSON.parse(localStorage.getItem('items')) || []
36+
const btnCheck = document.getElementById('btn-check')
37+
const btnUncheck = document.getElementById('btn-uncheck')
3238

3339
function addItem(e) {
3440
e.preventDefault()
@@ -65,8 +71,26 @@ <h2>LOCAL TAPAS</h2>
6571
populateList(items, itemsList)
6672
}
6773

74+
function toggleCheck(e) {
75+
if (e.target.matches('#btn-check')) {
76+
const inputs = itemsList.querySelectorAll('input')
77+
inputs.forEach((input, i) => {
78+
items[i].done = true
79+
})
80+
} else {
81+
const inputs = itemsList.querySelectorAll('input')
82+
inputs.forEach((input, i) => {
83+
items[i].done = false
84+
})
85+
}
86+
localStorage.setItem('items', JSON.stringify(items))
87+
populateList(items, itemsList)
88+
}
89+
6890
addItems.addEventListener('submit', addItem)
6991
itemsList.addEventListener('click', toggleDone)
92+
btnCheck.addEventListener('click', toggleCheck)
93+
btnUncheck.addEventListener('click', toggleCheck)
7094

7195
populateList(items, itemsList)
7296
</script>

‎15 - LocalStorage/style.css‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,20 @@
7676
outline:0;
7777
border:1px solid rgba(0,0,0,0.1);
7878
}
79+
80+
.add-items button {
81+
margin-top: 8px;
82+
border: none;
83+
padding: 5px;
84+
float: right;
85+
margin-left: 8px;
86+
border: 1px solid rgba(0,0,0,.12);
87+
outline: none;
88+
cursor: pointer;
89+
}
90+
91+
.clear-both {
92+
height: 0;
93+
clear: both;
94+
overflow: hidden;
95+
}

0 commit comments

Comments
 (0)