-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
58 lines (52 loc) · 2.24 KB
/
script.js
File metadata and controls
58 lines (52 loc) · 2.24 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
var friendlist = ["Larry", "Marry", "Garry", "Mike", "Zack", "Luccy"];
window.onload = build;
document.getElementById('addNew').addEventListener('click', addNewFriend,false);
function addNewFriend(){
var newFriend = document.getElementById('addFriend').value;
friendlist.push(newFriend);
build();
}
function build(){
var html = "<h1>Friend List table</h1>";
html += "<table>";
for(var i=0; i<friendlist.length; i++){
html += "<tr id='id" + i + "' data-row='" + i + "'>"
html += "<td>" + friendlist[i] + "</td>";
html += "<td><a href='#' data-action='delete'>Delete</a></td>";
html += "<td><a href='#' data-action='edit'>Edit</a></td>"
html += "</tr>";
}
html += "</table>";
document.querySelector('.output').innerHTML = html;
var dataActionDel = document.querySelectorAll('[data-action="delete"]');
for(var i=0; i<dataActionDel.length; i++){
dataActionDel[i].addEventListener('click', function(){
event.preventDefault();
var indexTrValue = this.closest("[data-row]").getAttribute("data-row");
friendlist.splice(indexTrValue, 1);
build();
});
}
var dataActionEdit = document.querySelectorAll('[data-action="edit"]');
for(var i=0; i<dataActionEdit.length; i++){
dataActionEdit[i].addEventListener('click', function(){
event.preventDefault();
var row = this.closest("[data-row]");
var rowId = row.getAttribute('data-row');
row.style.backgroundColor = "yellow";
var td = row.firstElementChild;
var input = document.createElement('input');
input.type = "text";
input.value = td.innerText;
td.innerHTML = "";
td.appendChild(input);
input.focus();
input.onblur = function(){
td.removeChild(input);
td.innerHTML = input.value;
friendlist[rowId] = input.value;
row.style.backgroundColor = "white";
}
});
}
}