-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
434df7d
commit 31adc4b
Showing
3 changed files
with
59 additions
and
111 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,48 @@ | ||
var newWord = document.getElementById("word-new"); | ||
var list = document.getElementById("word-list"); | ||
var count = document.getElementById("word-count"); | ||
var newWord = document.getElementById("word-new") | ||
var list = document.getElementById("word-list") | ||
var count = document.getElementById("word-count") | ||
|
||
function updateCount(){ | ||
if (list.children.length > 1) count.innerHTML = list.children.length + ' words' | ||
else if (list.children.length == 0) count.innerHTML = 'hit enter to add word' | ||
else count.innerHTML = list.children.length + ' word' | ||
} | ||
|
||
list.addEventListener("click", function(event) { | ||
document.addEventListener("click", function(event) { | ||
if (event.target !== event.currentTarget) { | ||
if (event.target.className == "destroy") | ||
{ | ||
event.target.parentElement.parentNode.removeChild(event.target.parentElement); | ||
updateCount(); | ||
event.target.parentElement.parentNode.removeChild(event.target.parentElement) | ||
updateCount() | ||
} | ||
if (event.target.className !== "edit" && document.querySelector('.edit')) | ||
{ | ||
var element = document.querySelector('.edit') | ||
var text = element.value | ||
element.parentNode.innerHTML = text | ||
} | ||
if (event.target.className == "listitem") | ||
{ | ||
if (document.querySelector('.edit')) return | ||
var text = event.target.innerHTML | ||
var input = document.createElement('input') | ||
input.className = 'edit' | ||
event.target.appendChild(input) | ||
input.value = text | ||
input.focus() | ||
input.setSelectionRange(0, input.value.length) | ||
} | ||
} | ||
event.stopPropagation(); | ||
}); | ||
event.stopPropagation() | ||
}) | ||
|
||
newWord.addEventListener("keypress", function(event) { | ||
if (event.keyCode == 13 && newWord.value.trim() !== '') | ||
{ | ||
var entry = document.createElement('li'); | ||
entry.innerHTML = '<label>' + newWord.value + '</label><button class="destroy"></button>'; | ||
list.insertBefore(entry, list.childNodes[0]); | ||
newWord.value = ''; | ||
updateCount(); | ||
var entry = document.createElement('li') | ||
entry.innerHTML = '<label class="listitem">' + newWord.value + '</label><button class="destroy"></button>' | ||
list.insertBefore(entry, list.childNodes[0]) | ||
newWord.value = '' | ||
updateCount() | ||
} | ||
}); | ||
}) |