-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_col3.js
83 lines (73 loc) · 2.68 KB
/
edit_col3.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
window.onload = function() {
const container = document.querySelector('#container');
// Retrieve current data from local storage
let three = JSON.parse(localStorage.getItem('three')) || [];
// Create a row for each item in the array
three.forEach((item, i) => {
// Create a form for the item
const form = document.createElement('form');
form.addEventListener('submit', (event) => {
event.preventDefault();
// Update the item's name and link
three[i].name = event.target[0].value;
three[i].link = event.target[1].value;
// Update local storage
localStorage.setItem('three', JSON.stringify(three));
});
// Create input fields for the item
const inputName = document.createElement('input');
inputName.type = 'text';
inputName.name = 'name';
inputName.value = three[i].name;
form.appendChild(inputName);
const inputLink = document.createElement('input');
inputLink.type = 'text';
inputLink.name = 'link';
inputLink.value = three[i].link;
form.appendChild(inputLink);
const button = document.createElement('button');
button.type = 'submit';
button.textContent = 'Submit';
form.appendChild(button);
container.appendChild(form);
// Create a button to delete the item
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.addEventListener('click', () => {
three.splice(i, 1);
localStorage.setItem('three', JSON.stringify(three));
window.location.reload();
});
form.appendChild(deleteButton);
container.appendChild(form);
});
// Create a form to add a new item
const addForm = document.createElement('form');
addForm.addEventListener('submit', (event) => {
event.preventDefault();
// Add a new item to the array
three.push({
name: event.target[0].value,
link: event.target[1].value
});
// Update local storage
localStorage.setItem('three', JSON.stringify(three));
window.location.reload();
});
// Create input fields for the new item
const addInputName = document.createElement('input');
addInputName.type = 'text';
addInputName.name = 'name';
addInputName.placeholder = 'Name';
addForm.appendChild(addInputName);
const addInputLink = document.createElement('input');
addInputLink.type = 'text';
addInputLink.name = 'link';
addInputLink.placeholder = 'Link';
addForm.appendChild(addInputLink);
const addButton = document.createElement('button');
addButton.type = 'submit';
addButton.textContent = 'Add';
addForm.appendChild(addButton);
container.appendChild(addForm);
};