Permalink
Cannot retrieve contributors at this time
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?
dynamic-websites/exercises/05_client/todo/script.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
53 lines (43 sloc)
1.29 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
let tableVisible = false | |
const itemCol = 0 | |
const qtyCol = 1 | |
const actCol = 2 | |
document.addEventListener('DOMContentLoaded', () => { | |
console.log('page loaded') | |
document.querySelector('input[name="item"]').focus() | |
document.querySelectorAll('input').forEach( elem => elem.onkeyup = addText ) | |
document.querySelector('form').onsubmit = addItem | |
document.querySelector('table').onclick = deleteRow | |
}) | |
function addText(e) { | |
const enterKey = 13 | |
console.log('keyup') | |
console.log(e.target.name) | |
if(e.which === enterKey) addItem(e) | |
} | |
function addItem() { | |
console.log('addItem') | |
const itemField = document.querySelector('input[name="item"]') | |
const item = itemField.value | |
itemField.value = '' | |
itemField.focus() | |
if(item.length) addNode(item) | |
return false // prevents the form from submitting | |
} | |
function clearInput() { | |
document.querySelector('input[name="item"]').value = '' | |
document.querySelector('input[name="item"]').focus() | |
} | |
function addNode(item, qty) { | |
const table = document.querySelector('table') | |
const row = table.insertRow(-1) | |
row.insertCell(0).innerHTML = item | |
row.insertCell(1).innerHTML = '<a href="#">delete</a>' | |
clearInput() | |
} | |
function deleteRow(e) { | |
console.log('delete row') | |
const index = e.target.parentElement.rowIndex | |
document.querySelector('table').deleteRow(index) | |
} |