-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9-41. Update & Delete HTML Elements.js
More file actions
60 lines (45 loc) · 1.69 KB
/
9-41. Update & Delete HTML Elements.js
File metadata and controls
60 lines (45 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Example code (Here i am not uploading HTML and CSS file to Github of this code)
/* Create HTML Element part 2 :
*/
var list = document.getElementById('todo-list');
var inputBox = document.getElementById('todo-input');
var btnAdd = document.getElementsByTagName('add-Item');
var btnUpdate = document.getElementById('update-item');
var btnRemove = document.getElementById('remove-item');
var currentInputValue = '';
inputBox, addEventListener('input', function (e) {
currentInputValue = e.target.value;
});
inputBox.addEventListener('keyup', function(e){
if(e.keyCode === 13){
addListItem();
}
});
function createNewNode (){
var newListElement = document.createElement('li')
var textNode = document.createTextNode(currentInputValue);
newListElement.appendChild(textNode);
newListElement.id = "item" + (List.childElementCount + 1);
return newListElement;
}
function addListItem() {
if (currentInputValue !== undefined && currentInputValue !== null && currentInputValue !== '') {
var newListElement = createNewNode();
list.appendChild(newListElement);
console.log(list.childElementCount);
inputBox.value = ''
currentInputValue = ''
}else {
alert('Please enter a valid item')
}
};
btnAdd.addEventListener('click', addListItem);
btnUpdate.addEventListener('click', function(){
var firstElement = list.firstElementChild;
var newListElement = createNewNode();
list.replaceChild(newListElement, firstElement);
});
btnRemove.addEventListener('click', function(){
var firstElement = List.firstElementChild;
list.removeChild(firstElement);
})